How to open file from storage by URL address?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Open Files from Storage Using a URL Address in Laravel

As developers working with frameworks like Laravel, understanding how to serve files stored on the server is fundamental. You've encountered a very common hurdle: trying to access a file directly via its filesystem path (e.g., /storage/file.txt) and expecting a browser to display it. Unfortunately, this method does not work because the web server doesn't inherently know how to translate that local path into a publicly accessible HTTP response.

This post will walk you through the correct, secure, and idiomatic way to serve files stored in Laravel’s storage directory via a URL address.

Why Direct Linking Fails

When you use a direct link like http://[temp.com]/storage/tests/filename.txt, you are essentially asking the web server to look for that path within its public document root. Unless you have specifically configured your application to map that exact filesystem path to a web route, the request will result in a 404 error because it is not a recognized endpoint.

Filesystem paths are internal to the server; URLs are how clients request data over the network. To bridge this gap, we need an intermediary layer—a Laravel route and controller—that reads the file content and explicitly sends it back to the user as an HTTP response.

The Correct Laravel Approach: Serving Files via Routes

The recommended way to make files accessible publicly in a Laravel application is by utilizing the Storage facade and defining a specific route to handle the request. For this to work seamlessly, you must ensure your files are stored on the public disk, which Laravel automatically links to the storage/app/public directory.

Step 1: Ensure Public Disk Configuration

First, ensure your storage configuration is set up correctly for public access. In your config/filesystems.php, the default setup generally handles this, but it’s good practice to confirm that you are using a disk configured for public access.

Step 2: Create a Route and Controller Method

We will create a route that accepts a file identifier (like a filename or path) and uses the Storage facade to retrieve the file contents before sending them to the browser.

Here is an example demonstrating how you might set up a simple endpoint to serve a file:

php
// app/Http/Controllers/FileController.php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;

class FileController extends Controller
{
    public function download(Request $request, $filename)
    {
        // 1. Define the path relative to the 'public' disk
        $path = 'tests/' . $filename; 

        // 2. Check if the file exists
        if (!Storage::disk('public')->exists($path)) {
            abort(404, 'File not found.');
        }

        // 3. Get the file content as a string
        $fileContent = Storage::disk('public')->get($path);

        // 4. Return the content with the correct headers for download/display
        return response($fileContent, 200, [
            'Content-Type' => 'text/plain', // Adjust MIME type as needed (e.g., 'image/jpeg')
            'Content-Disposition' => 'attachment; filename="' . $filename . '"'
        ]);
    }
}

Step 3: Define the Route

Next, you must define a route in routes/web.php that points to this controller method.

php
// routes/web.php

use App\Http\Controllers\FileController;

Route::get('/download/{filename}', [FileController::class, 'download']);

Now, when a user accesses a URL like http://your-app.test/download/de139607636857fade861a3c2c472643.txt, the controller executes, reads the file from storage, and streams the content back to the browser, successfully bridging the gap between your internal storage and the public web interface. This pattern is a cornerstone of building dynamic applications in Laravel, emphasizing clean separation of concerns, much like the principles outlined on laravelcompany.com.

Conclusion

Directly linking to arbitrary filesystem paths is not feasible for web serving. To open files from storage by URL address, you must implement an API endpoint. By defining a route that leverages Laravel's Storage facade and returns the file content as an HTTP response, you create a robust, secure, and scalable method for managing and delivering your application's assets. Always prioritize using structured routes and controllers over direct filesystem manipulation to maintain application integrity.