How to create disk at runtime and store file in Laravel?

In this article first we will know about Laravel driver and disk. Then we will know how to create disk at run time.

What is driver and disk in Laravel?

Laravel filesystem integration has drivers to work with local filesystems, FTP, SFTP, and Amazon S3.

In Laravel filesystem the supported Drivers are: “local”, “ftp”, “sftp”, “s3”. Here Laravel has local driver to manage local file system and s3 driver to manage s3 file system.

The disk is the specific storage driver and the location of the storage.

We can find Laravel filesystem configuration file in config/filesystems.php. Here We can configure all of our filesystem disks.

Driver and disk configuration

We can specify the default filesystem disk that is to be used by the framework here in config/filesystems.php.

// ...
'default' => env('FILESYSTEM_DISK', 'local'),
// ...

We may configure as many filesystem disks as we wish inside the disk array in config/filesystems.php.

Here we see the configuration of the disk array as follows.

// ...

'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
            'throw' => false,
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
            'throw' => false,
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
            'throw' => false,
        ],

    ],

// ...

The local driver is used to work with files that are stored on the server of the Laravel application.

The s3 driver is used to interact with Amazon’s S3 cloud storage service.

The FTP/ SFTP driver is used to interact with other storage locations outside the application.

We can learn more about Laravel File Storage at https://laravel.com/docs/10.x/filesystem.

We will now know that whatever the default filesystem disk is in our application, we can change it at run time to manage files in the path as per our convenience.

Create disk at runtime

To do this, we can pass a configuration array to the Storage facade’s build method in our controller file.

We can follow the steps below to implement this.

Step1: Creating a new controller

We will create a controller named RuntimeDiskController by the following artisan command.

php artisan make:controller RuntimeDiskController

Step2: Adding code in the newly created controller

We will add following code in the newly created controller named RuntimeDiskController. In the store method we will use the build method of Laravel’s storage facade.

<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Storage;
class RuntimeDiskController extends Controller
{
public function store(){
$disk = Storage::build([
'driver' => 'local',
'root' => $_SERVER['DOCUMENT_ROOT'].'/run_time',
]);
$disk->put('example.txt', 'Contents');
}
}

Step3: Creating a route

Now we will add the following get route in routes\web.php.

<?php
use App\Http\Controllers\RuntimeDiskController;
use Illuminate\Support\Facades\Route;
Route::get('/store', [RuntimeDiskController::class, 'store']);

Now if we open http://<SITEURL>/store in browser, example.txt file with content ‘Contents’ will be stored in the run_time directory of the installation path of Laravel application.

Now I will explain how the file is saved in the above path. If we browse this URL http://<SITEURL>/store, the store method of RuntimeDiskController will be called according to the route declaration.

In the store method, we created the disk at runtime using the build method of Laravel’s storage facade.

During this disk creation, we have specified the disk driver through the ‘driver’ key and the path of the disk through the ‘root’ key.

Then using the put method we have created a file named example.txt on this disk and its content is ‘Contents’.