Tutorialsplane

Download files in Laravel


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);

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.

Laravel General

pagination

Laravel More