Laravel - link to a file saved in storage folder
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
When working with Laravel, there are several ways to store files and make them accessible within your application. The storage folder is a built-in feature that allows you to store assets safely on the server. In this blog post, we will discuss the process of generating links to these stored files from a view. To address the issues brought up in the comment section, we'll take a closer look at how to create and format these links.
Firstly, let's review the basics. The storage folder is located within your application directory and can be accessed by using Laravel's Storage facade. With this, you can use Storage::disk('local') as shown in the example code. This method puts a file with name 'file.txt' that contains the text "Contents" into the local disk. Now, let's focus on generating links to those files from within your views.
Creating links in Laravel is quite straightforward. You can use the url() helper function to generate a URL based on an array of parameters or a closure containing the path to navigate. In our case, you want to link to the file stored within the storage folder. We'll need to create a route that uses this file as a resource. Here's how it can be done:
Route::get('download/{filename}', function ($filename) {
$storageFile = Storage::disk('local')->get($filename);
return response()->file($storageFile);
});
This route will retrieve a file from the storage folder based on the filename and respond with it. Now that we have this route in place, we can create links to these files within our views.
In your view, you can use the url() helper function to generate the link:
<a href="{!! route('download_file', ['filename' => 'file.txt']) !!}">Download File</a>
Remember that you need to replace the placeholder "download_file" with your actual route name. This will ensure the link points to the correct file. To create a more dynamic link, you can also store all the filenames in an array and cycle through them using Laravel's @foreach directive:
<a href="{!! route('download_file', ['filename' => $files[$loop->index]]) !!}">Download File #{{$loop->index + 1}}</a>
By using this approach, you can easily generate links to files stored in Laravel's storage folder. It ensures that your application follows proper security practices while providing easy access to these assets for your users.
Laravel Company offers comprehensive tutorials and resources on topics like this and many others. For more information on working with files and routes, make sure you visit our website at https://laravelcompany.com/articles/tag/laravel-filesystem to dive deeper into these concepts.
In conclusion, creating links to files stored in the storage folder of your Laravel application is a straightforward process once you understand how the Storage facade and route generation work. By adopting best practices and keeping security in mind, you can provide easy access to your users with minimal effort.