In this article, I'll show you how to display images stored in Laravel's storage
folder in your Laravel 11 application. Laravel provides a simple way to handle image files by storing them securely in the storage/app/public directory and accessing them using a public link.
By following these steps, you'll be able to display stored images in your Laravel Blade templates or anywhere else in your app.
Laravel 11 Display Image from Storage Folder
To make the storage
folder accessible via the web, Laravel requires a symbolic link (symlink) from public/storage
to storage/app/public
Run the following Artisan command to create the symlink:
php artisan storage:link
This command creates a symlink at public/storage
pointing to storage/app/public
. Now, any files saved in storage/app/public
will be accessible through the URL path public/storage
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class HomeController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$imageUrl = Storage::url('images/test.jpg');
}
}
Display the Image in your Blade view:
<img src="{{ Storage::url('images/test.jpg') }}" alt="Image">
In web.php
, define a route to display images.
Route::get('image/{filename}', 'ImageController@displayImage')->name('image.displayImage');
app/Http/Controllers/ImageController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class ImageController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function displayImage()
{
$path = 'public/images/' . $filename;
if (!Storage::exists($path)) {
abort(404);
}
$file = Storage::get($path);
$type = Storage::mimeType($path);
return response($file, 200)->header('Content-Type', $type);
}
}
In your Blade template, use this route to access the image by its filename:
<img src="{{ route('image.displayImage',$image_name) }}" alt="" title="">
Laravel recommends storing images in the storage/app/public
directory so they can be accessed publicly. You can also organize images in a subfolder, such as images
, for better structure.
// Saving an uploaded image in a controller
$path = $request->file('image')->store('public/images');
// Get the Path for Display
$path = $request->file('image')->store('public/images');
$url = Storage::url($path); // To get the accessible URL for the stored image
You might also like: