Skip to main content
PHP Blog

Back to all posts

How to Convert Stream to Download In Laravel?

Published on
4 min read
How to Convert Stream to Download In Laravel? image

Best Laravel Stream Conversion Tools to Buy in October 2025

+
ONE MORE?

To convert a stream to a download in Laravel, you can use the response() method with the appropriate headers to force the browser to download the file instead of displaying it in the browser. First, get the stream content using Storage::get() or any other method that returns a stream. Then, use the response() method to create a new response and set the headers to force a download. For example, you can return a response like this:

return response()->streamDownload(function () use ($stream) { fpassthru($stream); }, $filename);

Replace $stream with the stream content and $filename with the desired filename for the downloaded file. This code will send the stream content to the browser as a download, allowing the user to save the file to their device.

How to create a downloadable file from a stream in Laravel?

To create a downloadable file from a stream in Laravel, you can follow these steps:

  1. Create a route in your Laravel application to handle the download request:

Route::get('/download', 'DownloadController@download');

  1. Create a controller called DownloadController using the following command:

php artisan make:controller DownloadController

  1. In the DownloadController, create a method called download that returns the downloadable file. You can use the response()->stream() method to stream the file content: