Laravel Response Download File Example
We sometimes require to return response with download file from controller method like generate invoice and give to download or etc. Laravel provide us response() with download method that way we can do it.
you can also response download file from storage and delete it after download in laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 version.
In First argument of download() we have to give path of download file. We can rename of download file by passing second argument of download(). We can also set headers of file by passing third argument.
In bellow example will help you how it is works.
So, first i am going to create new route for our example as like bellow:
routes/web.php
Route::get('donwload-file', 'HomeController@downloadFile');
Ok, now i have to add one method "downloadFile()" in my HomeController. If you don't have HomeController then you can use your own controller as like bellow:
app/Http/Controllers/HomeController.php
Example 1: Download File from Storage
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class HomeController extends Controller
{
public function downloadFile()
{
$myFile = storage_path("folder/dummy_pdf.pdf");
return response()->download($myFile);
}
}
Example 2: Download File with name and headers
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class HomeController extends Controller
{
public function downloadFile()
{
$myFile = public_path("dummy_pdf.pdf");
$headers = ['Content-Type: application/pdf'];
$newName = 'itsolutionstuff-pdf-file-'.time().'.pdf';
return response()->download($myFile, $newName, $headers);
}
}
Example 2: After Download File will remove
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class HomeController extends Controller
{
public function downloadFile()
{
$myFile = storage_path("folder/dummy_pdf.pdf");
return response()->download($myFile)->deleteFileAfterSend(true);
}
}
So, maybe it can help you....

Hardik Savani
I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Livewire Datatables Example Tutorial
- Laravel 8 Multi Auth (Authentication) Tutorial
- How to Delete File from Public Folder / Storage Folder in Laravel?
- Laravel Move File from One Folder to Another Example
- Laravel Copy File from One Folder to Another Example
- Laravel - generate PDF from html view file and download using dompdf
- Laravel - Multiple Files Download with Response Example
- Laravel json response from controller example
- Laravel Create JSON File & Download From Text Example