Download files in Laravel : Downloading files from url is very easy in laravel. Response::download($filePath)is used to download files in laravel. This generates a response which forces users browser to download the file from the specified path. Here in this tutorial we are going to explain the download files.
Download files in Laravel
Here in syntax for downloading files in laravel.
Syntax
Syntax for Downloading files in Laravel 5 from url:
return response()->download($pathToFile, $name, $headers); |
- $pathToFile : Full Path of file to be downloaded.
- $name : Name is optional. This is name which user will see after downloading the file.
- $headers : Headers are also optional. You can pass an array of HTTP headers.
Example
Here is a simple function which contains the code to download files in laravel using the Response::download() .-
Download files in Laravel 5 from url Example
public function fileDownload(){ //Suppose profile.docx file is stored under project/public/download/profile.docx $file= public_path(). "/download/profile.docx"; $headers = array( 'Content-Type: application/octet-stream', ); return Response::download($file, 'my-filename.docx', $headers); } |
The above example will download the file with name my-filename.docx on user’s machine. Both second and third parameters are optional.