Brief introduction to Laravel5 download file Laravel 5 download file
Laravel provides many methods for downloading files. you can use Stroage. Laravel 5's own file system is very powerful. It provides many methods and supports remote storage. You can also use Response directly. laravel 5's Response is also based onSyfmonyFurther encapsulation of Symfony \ Component \ HttpFoundation \ Response.
Practice truth
Yesterday I made a file download function. I thought it was necessary to use Stroage to complete all operations. I checked the official website documentation and found that the download method of Response was used to download the file, in this case, why not directly use the download method of Response? Of course, the premise is that I already know the absolute or relative path of the file.
The default Storage path is in the/storage/app Directory. if you do not set the path, you can use the method in Storage to obtain or save files in this directory.
But the file I saved is not in this directory. if there is an absolute path, the easiest way is to directly use the download method.
If it is a remote ftp, you have to use Storage, select the ftp service, get it, and pass in the Response: download () method. the usage is similar to the following.
return response()->download($resource['path'] . $resource['name'], $resource['original_name']);
Let's take a look at the download method:
/** * Create a new file download response. * * @param \SplFileInfo|string $file * @param string $name * @param array $headers * @param string|null $disposition * @return \Symfony\Component\HttpFoundation\BinaryFileResponse */
public function download($file, $name = null, array $headers = [], $disposition = 'attachment');
- The first is the absolute path of the file or the file (required) \ SplFileInfo or string type.
- The second is the file name saved after Download (optional). If this parameter is left blank, the file name saved after download will be of the same string type as the file name saved on the server.
- The second is the header (optional) array type.
- What is the third description? I have never tried this either.
In this case, it's so easy to download files. let's simply write it. there are too many laravel tutorials online. In fact, I don't really want to write laravel, because I don't like this framework very much, it is very convenient.