Download files from storage in Laravel

2197
Download files from storage in Laravel

A filesystem is often an underrated but very important factor in web development. Downloading files from storage in laravel application is even easier with the implementation of response or download method. This feature was added by reinink and merged by taylor in laravel 5.5.

“The download method accepts a file name as the second argument to the method, which will determine the file name that is seen by the user downloading the file. Finally, you may pass an array of HTTP headers as the third argument to the method” as described in the documents.

Example:

/**
 * Automatically return a file from any filesystem as an HTTP response
 * You can optionally set a filename, headers, and disposition
 * @param  User   $user [description]
 * @return [type]       [description]
 */
public function photo(User $user)
{
    return Storage::response($user->photo);
}

/**
 * You can also use the download() method to force download a file
 * @param  User   $user [description]
 * @return [type]       [description]
 */
public function resume(User $user)
{
    return Storage::download($user->resume, 'resume.pdf');
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here