How to display image from storage folder in Laravel?

By Hardik Savani | November 12, 2019 | | 3769 Viewer | Category : Laravel


In this example, i will give you example of laravel display image from storage folder. we can easily show image from storage folder in laravel 6 application. we will display image from storage folder in blade file of laravel 6 project.

All most developer and client want to store images or files secure on his server. so anyone can not access directly using url. that's the reason laravel introduce storage folder file upload. Almost using storage folder for uploading files and images on his laravel app. But when you upload it also might be require to display or download for that image or file.

However, In this example, i will give you 2 way to show your image in blade file.

Solution 1:

first of all if you want to show image using asset function than you need to link your storage folder using following command:

php artisan storage:link

Now you can write image upload logic on your controller and image upload.

If you want to display image on your blade file than you can use asset function as like bellow example:

<img src="{{ asset(.'/images/'.$article->image) }}" alt="" title="">

Solution 2:

In second solution, we have to create route for display image on your application. so let's create route as like bellow:

Create Route:

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

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

}

Now you can use like as bellow:

<img src="{{ route('image.displayImage',$article->image_name) }}" alt="" title="">

You can use anyone.

I hope it can help you...




Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: