Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Discover File Downloads in Laravel with Storage::download

    Discover File Downloads in Laravel with Storage::download

    December 20, 2024

    Discover File Downloads in Laravel with Storage::download

    Laravel’s Storage::download simplifies secure file serving by providing a clean API for handling downloads while managing file storage abstraction.

    <?php
    
    namespace AppHttpControllers;
    
    use IlluminateSupportFacadesStorage;
    
    class FileController extends Controller
    {
        public function download($filename)
        {
            return Storage::download(
                "documents/{$filename}",
                "custom-{$filename}",
                ['Content-Type' => 'application/pdf']
            );
        }
    }
    

    Here is an example of using the Storage::download() in a sample controller:

    <?php
    
    namespace AppHttpControllers;
    
    use AppModelsDocument;
    use IlluminateHttpRequest;
    use IlluminateSupportFacadesStorage;
    
    class DocumentController extends Controller
    {
        public function download(Request $request, Document $document)
        {
            if (!$request->user()->canDownload($document)) {
                abort(403);
            }
    
            if (!Storage::exists($document->path)) {
                abort(404, 'File not found');
            }
    
            $document->increment('download_count');
    
            return Storage::download(
                $document->path,
                $document->original_name,
                [
                    'Content-Type' => $document->mime_type,
                    'Content-Disposition' => 'attachment',
                    'Cache-Control' => 'no-cache, must-revalidate'
                ]
            );
        }
    }
    

    All in all, Storage::download provides a secure, efficient way to serve files while abstracting storage provider details.


    The post Discover File Downloads in Laravel with Storage::download appeared first on Laravel News.

    Join the Laravel Newsletter to get all the latest
    Laravel articles like this directly in your inbox.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleLaravel whenLoaded – Performance Optimization via Conditional Relationship Loading
    Next Article Converting Collections to Queries in Laravel Using toQuery()

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 16, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-47916 – Invision Community Themeeditor Remote Code Execution

    May 16, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Dragon Age: The Veilguard director has left the studio after 18 years for an opportunity she, “couldn’t turn down” in the RPG space

    News & Updates

    Avowed: How to switch from first-person to third-person perspective

    News & Updates

    Critical Commvault Flaw Rated 10/10: CSA Urges Immediate Patching

    Security

    Exporting Test Cases with Attachments?

    Development

    Highlights

    Linux

    Red Hat In-Vehicle OS raggiunge un nuovo livello di compatibilità per portare Linux sulle automobili

    January 8, 2025

    Uno dei settori in cui Linux sta emergendo come tecnologia di riferimento, tolto il discorso…

    Catch two of Capcom’s best Monster Hunter titles on PC for massive discounts to tide you over while waiting for Monster Hunter Wilds

    June 21, 2024

    Better Together: Amazon SageMaker Canvas and RDS for SQL Server, a predictive ML model sample use case

    August 7, 2024

    Octo: An Open-Sourced Large Transformer-based Generalist Robot Policy Trained on 800k Trajectories from the Open X-Embodiment Dataset

    May 24, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.