How to get file URL using Storage facade in laravel 5?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Retrieving File URLs using Storage Facade in Laravel 5 - A Comprehensive Guide Introduction: As you have begun experimenting with Flysystem integration in Laravel 5, you might be wondering how to retrieve the file URL (full path) using the Storage facade to display images or other files on your application. In this blog post, we'll dive deeper into the concept and provide a thorough answer while sharing relevant code examples and best practices. Getting Started: Before jumping into the steps of retrieving file URLs using the Storage facade, let us first understand how you are storing the paths in your database. Ideally, you have a table with fields for the media type (screenshots in this case), name (1.jpg), and path (screenshots/1.jpg). Here's an example of such a table: | Media Type | Name | Path | |------------|---------|---------------- | Screenshots| 1.jpg | screenshots/1.jpg To be able to retrieve the file URL from this structure, we can use the Storage facade's methods - specifically 'disk()', 'get()', and 'url()'. Here is a walkthrough of how these functions work: Disk Function: The disk function returns the provided disk name and its instance. You can call it like this:
$disk = Storage::disk('local');
Here, 'local' represents the disk you want to use, which could be local storage or a cloud service such as Amazon S3. The $disk variable now contains the instance of the selected disk. Get Function: The get function is used for retrieving the contents of a file when needed in your application. You can call it like this:
$contents = $disk->get('screenshots/1.jpg');
This code will return the entire contents of the image '1.jpg' stored under the screenshots folder on the local disk or your preferred storage option. However, as you mentioned in your previous response, you primarily want to display images on your views using tags. To achieve that, we'll utilize the url function: Url Function: The url function takes a file path and generates the URL for displaying it on webpages. If you have the full URL (path) stored in your database, you can use the following code to fetch the correct image source:
<img src="{{ Storage::url($media_file_path) }}" />
Here, $media_file_path represents the actual path (screenshots/1.jpg in this case) you have stored in your database. Calling the url function will generate the correct URL for displaying the image on your views. Why is it Designed This Way? The reason behind not having a direct method to get the full file URL from the path (like Storage::path()) might be related to security and performance concerns. By separating the content retrieval and URL generation, Laravel can provide better control over caching and access permissions. Additionally, different storage mechanisms may have varying requirements, and using separate functions allows for more flexibility in handling these differences. Conclusion: In conclusion, there are two distinct methods provided by the Storage facade to retrieve file contents (get) and generate URLs (url). While it might seem a bit roundabout compared to having one function that does both, this approach helps maintain security and performance. Always remember to optimize your workflow for better user experience and development efficiency. Best Practices: - Store 'localized' paths in the database using clear naming conventions (e.g., screenshots/1.jpg). - Use the get function to retrieve the file contents when needed. - Utilize the url function to generate the correct image source for your views. - Remember to secure access to these files via appropriate authentication and permissions. - Consider using the Laravel Mix asset management tool for better performance and simplicity when handling assets. Resources: To learn more about using the Storage facade in Laravel 5, refer to the official documentation at https://laravel.com/docs/5.3/filesystem#disk-configuration and explore other helpful resources on Laravel Company's website (https://laravelcompany.com). Additionally, feel free to experiment with different storage mechanisms such as Amazon S3 or Google Cloud Storage for better performance and scalability. In conclusion, you can indeed get the file URL using the Storage facade while maintaining security and performance. By using the appropriate functions and best practices, you will achieve a stable and efficient solution for displaying images on your Laravel application.