Force download file
If you are making a web application using laravel and you need to download some file like picture or report or statement.So how can you download the file forcefully from controller in laravel?You know download file is common things for an application.
In this tutorial, I will tell you about the download() method that is used to generate a response to force download the file in your Laravel application.
This download method accept the three arguments.In first argument, You will pass the path of download file and the second argument will hold the file name, Finally the third argument hold the HTTP headers. You can change the filename of download file by passing it as a second argument in download().
Now lets go to code section.
Now to download the file we need to create a route for that.
Here first parameter is the route name which is download.
Second parameter holds controller name and method name.
Now add following code in your MyController.php
class MyController extends Controller
public function myDownload()
$filepath = public_path("report.pdf");//Get file absolute path
$header = ['Content-Type: application/pdf'];
$filename = "myreport".time().'.pdf';
return response()->download($filepath, $filename, $header);
We can download a file when click on a link having download attribute.It is also very easy in laravel 5.
You can check my previous tutorial of file upload to amazon aws using laravel.