Laravel Retrieve Images from storage to view
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Laravel provides a robust file storage system that offers several ways to manipulate and display uploaded images. In this article, we'll discuss how you can efficiently retrieve images stored in Laravel for viewing by users.
Storing Uploaded Files
$file = $request->file($file_attachment);
$rules = [];
$rules[$file_attachment] = 'required|mimes:jpeg|max:500';
//...
$uploaded = Storage::put(
$destinationPath . $file_attachment . '.' . $file->getClientOriginalExtension(),
file_get_contents($file->getRealPath())
);
As seen in the code above, you use Laravel's storage system to store uploaded files. The file is first validated before being stored in a specified destination path. Laravel then saves the image with its original extension under that given path.
Retrieving and Displaying Images
When you want to show your users the images they've uploaded, you can use the following code:
<img src="{{ URL::asset('storage/photo.jpg') }}" />
Here, the image is displayed as an img tag, and its source URL is set to a Laravel-provided helper function, URL::asset(), which returns a fully qualified URL to your uploaded image's storage location. This ensures that the images are served correctly and securely.
Avoiding Unreadable Texts
You mentioned experiencing unreadable texts when trying to retrieve a file from storage. This is often caused by an incorrect content type header being sent along with the image data. To avoid this issue, you can use the following code in your controller:
$storage = Storage::get('/2/filename.jpg');
return response($storage)
->header('Content-Type', 'image/' . explode('.', $file_attachment)[1]) // Set the correct content type header based on file extension
->setCallback(function ($response, $file) {
return $response->withAddedHeader('Access-Control-Allow-Origin', '*');
});
In this code, you retrieve the image from Laravel storage and set its content type header to the appropriate file extension. Additionally, a callable is attached to your response object, which allows you to add custom headers for better compatibility with browsers.
Displaying Images on a Public Route
If you want users to access your images directly from their browser URLs, you can set up a dedicated route. Here's an example:
Route::get('storage/{filename}', function ($filename) {
$userid = session()->get('user')->id;
return Storage::get($userid . '/' . $filename);
});
});
This route allows you to access images based on their file name. It ensures that the correct user's uploaded images are displayed, preventing unauthorized access and enhancing security.
Conclusion
Retrieving and displaying uploaded images from Laravel storage is a crucial aspect of your application. By following these guidelines, you can easily show your users their uploaded pictures while maintaining the necessary security measures. Additionally, ensure that your image content-type headers are set correctly to provide a smooth user experience.
Note: Remember to keep your code well-structured and easy to read for better maintainability and collaboration. Always follow Laravel's best practices when working with files and images to ensure the best performance.