Laravel: How to create temporary URL for files in Local Disk

Sharing temporary local file URLs or Links in Laravel has never been easy. We all have those secret files that we only people authorized to access them and we have a lot of services to help us with that such as DigitalOcean Cloud, S3 and what not.

But when it comes to local development where you don’t want to use a third-party cloud storage system just to develop the website or just to be a little more faster with serving the files or even for production where you have a small load and don’t want to bother using a third-party cloud storage system, it can be an issue for some developers, this blog post will help you with how you can create temporary urls for the files that are on your local storage.

This article assumes that you are using at least the v11.23 version of Laravel since the examples here uses the latest version of Laravel.

In order to serve your files in local disk with a temporary URL, we need to make some changes starting with making sure that your .env file is using the correct driver (local). Here’s how your .env file should look like:

FILESYSTEM_DISK=local

After you have confirmed that part or have changed it, now there is another small change we will need to do in our filesystems.php configuration file. Open config>filesystems.php file and then search for your ‘local’ (the first element) in the ‘disks’ array and under the ‘local’ array, you might see a ‘serve’ element, if you don’t see it, you can simple add it and make it’s value to true. Here’s an example:

'local' => [
    'driver' => 'local',
    'root' => storage_path('app'),
    'serve' => true, # Change this to true if it's false in your case (add it line if it's not there)
    'throw' => false,
],

Once you have marked serve element to true, now we are ready to create temporary url for our files.

Now that you have done all the configurations, let’s see how you can create a temporary URL in your application. In the example below, I’m creating a temporary URL that will expire in 5 seconds for a file that is stored in my storage>app>private directory. We will use the ->temporaryUrl() method of Storage Fascade to achieve this. Here’s the example:

use \Illuminate\Support\Facades\Storage;

Route::get('/my-page', function (){
    return Storage::temporaryUrl(
        'private/car.jpg', now()->addSeconds(5)
    );
});

With this example, when you visit /my-page of your website, you will see a URL which is about to expire in 5 seconds, so you better open that link quickly and see the file. This is using Laravel’s signed URLs with expiration time, so if you open that file after 5 seconds, you will see a 403 forbidden message.

If you are using a third-party service for cloud storage, In some rare cases where you need to create a temporary URL frequently for the same user, I recommend to cache your temporary url so that you don’t have to create it every time (for example a page refresh).

And that’s it! You can create as many link for as many file with as much expiration time you want anywhere in your application. It’s that easy!

If you liked the content and want support this site, you can do so by buying me a coffee.

2 thoughts on “Laravel: How to create temporary URL for files in Local Disk”

Leave a Comment