Laravel Public url for storage files

Laravel Public url for storage files

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel Public URL for Storage Files

When building a Laravel application, you often need to serve files stored in the storage directory directly to the browser rather than returning them as raw base paths like /storage/.... This is crucial when integrating with external services, generating thumbnails, or serving documents from a public-facing route. The standard approach involves configuring your environment correctly and using Laravel's helper functions to construct absolute URLs dynamically.

Configuring Storage Links

Before attempting to retrieve a public URL, you must ensure the symbolic link between storage and public/storage exists. While this is often set up during installation, it can be missing in local development environments or cloned repositories. You can verify this by checking if the public/storage directory contains your files from storage/app/public.

Run the following command to establish the link:

php artisan storage:link

This creates a symbolic link so that requests to /storage/... are served from the storage directory. Without this, any attempt to access these files via a browser will result in a 403 Forbidden or 404 Not Found error, regardless of your code logic.

Retrieving Public URLs with Storage::url()

Once the link is established, you should not construct paths manually using string concatenation. Instead, leverage Laravel's Storage facade. The method Storage::disk('public')->url($path) generates a full URL based on your application's base domain and the specific path relative to the public root.

Consider this scenario where you store a file in the spares directory:

// Store a file
$disk = Storage::disk('public');
$path = $disk->putFileAs('spares', 'some-image.jpg', 'image1.jpg');

// Get the public URL
$url = $disk->url($path); // Returns /storage/spares/image1.jpg

If you need the absolute URL including your domain (e.g., https://your-domain.com/storage/...), you can pass an optional base parameter or rely on Laravel's configuration to ensure the full path is generated correctly depending on your environment setup. For local development, this often results in a URL starting with http://localhost.

Listing Files and Constructing URLs Dynamically

If you need to retrieve a list of all files within a specific directory and generate their public links, use Storage::disk('public')->allFiles(). This returns a flat array of relative paths. You can iterate through this list and map each path to its corresponding URL.

Here is how you might fetch and transform these paths into absolute URLs:

$files = Storage::disk('public')->allFiles('spares');

$urls = [];
foreach ($files as $file) {
    // Ensure the path includes the disk root if necessary, 
    // though allFiles usually returns relative paths.
    $url = Storage::disk('public')->url($file);
    
    // If you need to include the domain explicitly for external use:
    $fullUrl = url('/' . $url); 
    
    $urls[$file] = $fullUrl;
}

return response()->json($urls);

Using the url() helper here ensures that the generated link respects your application's configured base URL, making it robust for both local and production environments.

Handling Metadata like Last Modified Dates

A common requirement when serving files is providing metadata such as the last modified timestamp to a frontend view or API response. You can combine file listing with metadata retrieval using the File facade (available via the illuminate/filesystem package).

When retrieving a list of files and their modification times, be mindful of how you associate data keys. In your example, iterating over $docs while simultaneously building a new associative array is valid, but ensure the loop logic correctly maps the original file path to the metadata.

use Illuminate\Support\Facades\File;

$docs = Storage::disk('public')->allFiles('document'); // Adjust disk and path as needed
$lastModified = [];
$urls = [];

foreach ($docs as $file) {
    // Retrieve absolute URL for the file
    $absoluteUrl = Storage::disk('public')->url($file);
    
    // Get last modified timestamp relative to storage root
    $modified = File::lastModified("storage/{$file}");
    
    $lastModified[$file] = $modified;
    $urls[$file] = $absoluteUrl;
}

return view('stock.document', compact('urls', 'lastModified'));

This approach keeps your data structured, allowing your view to render a table or list showing each file's download link alongside its modification date. Always ensure that the path passed to File::lastModified matches the actual location of the file on the disk, accounting for the storage link configuration.

Our network