How to display image from storage path using route in Laravel?

December 4, 2018 | Category : Laravel 5 Laravel PHP

In this tutorial, i will show you how to access and display of storage folder image or file using route in laravel 5 application.

we will create one route for access storage path for display image in admin side. So we can also give auth middleware to access only logged in user.

laravel provide storage folder for store all images and files for security, so basically someone can not access directly from url. but if you want to display then you can't display also without link. here we will display files using route.

Route:

Route::get('image/{filename}', 'HomeController@displayImage')->name('image.displayImage');

Controller Method:

public function displayImage($filename)

{

$path = storage_public('images/' . $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;

}

I hope you found your best...