Laravel Display Image From Storage Folder

Laravel 6 , Laravel , Laravel 7 , Laravel 8

Nicesnippets

730

08-10-2020


Laravel Display Image From Storage Folder

Hi Guys,

In this example,I will learn you how get image from storage folder in laravel.you can easy and simply get to image from storage folder in laravel.

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.

Example :


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

Create Route:

use App\Http\Controllers\StorageFileController;

Route::get('image/{filename}', [StorageFileController::class,'getPubliclyStorgeFile'])->name('image.displayImage');

Create Controller Method:

public function getPubliclyStorgeFile($filename)

{

$path = storage_path('app/public/image/'. $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',$test->image_name) }}" alt="" title="">

You can use anyone.

I hope it can help you...


Recommended Posts