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 filenamestreamDownload(): Streams dynamic output directly to browser with 0 disk storage overheadfile(): Renders image or PDF inline inside browser tab
Related Tips
View all tips →Stream CSV Downloads in Laravel Without Creating Temporary Files
Combine response()->streamDownload(), php://output, and query chunking to stream CSV exports directly to the browser without writing temporary files to disk.
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.