← BACK
Laravel / HTTP & API

Stream and Download Files Efficiently in Laravel Controllers

Punyapal Shah • • 1 min read
edit this tip →

Return proper HTTP file responses using response()->download(), response()->streamDownload(), and response()->file() for downloads and inline previews.

Serving files to users requires setting correct HTTP disposition headers and content types.

Laravel provides explicit response helpers for file handling:

use Illuminate\Support\Facades\Storage;

// Download local file directly
public function downloadContract()
{
    return response()->download(storage_path('app/contract.pdf'), 'Agreement.pdf');
}

// Stream generated content without storing to disk
public function exportCsv()
{
    return response()->streamDownload(function () {
        echo "id,name\n1,John";
    }, 'users.csv');
}

// Display file inline in browser (e.g. PDF preview)
public function previewInvoice()
{
    return response()->file(storage_path('app/invoice.pdf'));
}
  • download(): Triggers browser save dialog with custom filename
  • streamDownload(): Streams dynamic output directly to browser with 0 disk storage overhead
  • file(): Renders image or PDF inline inside browser tab

Related Tips

View all tips →
Laravel HTTP & API

Laravel 13.25 Reverses Merge Order in Request::all()

In Laravel 13.25, Request::all() now gives submitted input precedence over uploaded files when their keys collide, while file() continues to provide direct access to uploaded files.

READ >

// Got a tip in mind? Contributions are always welcome