cyberfly
7/24/2017 - 9:41 AM

Laravel file download route

Laravel file download route

Route::get('images/{filename}', function ($filename)
{
    $path = storage_path() . '/img/' . $filename;

    if(!File::exists($path)) abort(404);

    $file = File::get($path);
    $type = File::mimeType($path);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);

    return $response;
});
//To download any file that reside on your storage/app directory, the download response code should be like:

return response()->download(Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix().'/path-to-file';