How to display image from storage folder in Laravel?

Posted by

To display an image from the storage folder in Laravel, you can follow these steps:

  1. Save the image in the storage folder: Upload the image file to the storage/app/public directory or any other location within the storage folder. You can use the Storage facade to store the file. For example:

use Illuminate\Support\Facades\Storage;

// ...

$imagePath = $request->file('image')->store('public/images');

2. Create a symbolic link: Laravel does not allow direct access to files stored in the storage folder for security reasons. To make the images accessible, you need to create a symbolic link from the public folder to the storage folder. Run the following command in your terminal:

php artisan storage:link

This will create a symbolic link from public/storage to storage/app/public.

3. Display the image in a view: In your view file, use the asset() helper function to generate the URL for the image. The asset() function generates the full URL to the image using the symbolic link. For example:

<img src="{{ asset('storage/images/image.jpg') }}" alt="Image">

Replace storage/images/image.jpg with the actual path to your image file within the storage folder.

By following these steps, you can display an image from the storage folder in Laravel. The symbolic link ensures that the image can be accessed through a publicly accessible URL, allowing it to be displayed in your views.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x