Laravel Tutorials

Discover File Downloads in Laravel with Storage::download

Published
Discover File Downloads in Laravel with Storage::download image

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

<?php
	
namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;

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 App\Http\Controllers;

use App\Models\Document;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

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.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Generate Laravel Validation Rules From a JSON Schema image

Generate Laravel Validation Rules From a JSON Schema

Read article
Laravel Suspicious Login: Detect and Block Risky Logins image

Laravel Suspicious Login: Detect and Block Risky Logins

Read article
Decide with Jev: Make Failed AI Requests Safe in Laravel image

Decide with Jev: Make Failed AI Requests Safe in Laravel

Read article
Black Friday: Learn the habits behind AI workflows that work image

Black Friday: Learn the habits behind AI workflows that work

Read article
Laravel Black Friday and Cyber Monday Deals 2026 image

Laravel Black Friday and Cyber Monday Deals 2026

Read article
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article