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...
Do you like below Tutorials ?
- Angular Use CKEditor Example Tutorial
- How to Use Multiple Select Dropdown in Laravel?
- Angular 9 Image Upload with Crop Example
- Files Upload on Button Click in PHP Dropzone Example
- PHP Dropzone Allow only One File to Upload Example
- PHP Dropzone Allow only Images to Upload Example
- PHP Dropzone Delete Uploaded File Example
- Livewire CRUD Laravel 7 Example