How to Create Storage Disc in Laravel

AuthorSumit Dey Sarkar

Pubish Date22 Mar 2023

categoryLaravel

In this tutorial we will learn how to create storage disc in laravel.

 

How to create storage disc in laravel

In Laravel, you can create a storage disc by using the Storage facade.

Follow the below steps to create a new storage disk:

 

Step 1 - Open the config/filesystems.php file in your Laravel project.

 

Step 2 -  Add a new disk configuration under the disks array.

For example-

To create a disk called my_disk with local driver and store files in storage/app/my_disk then you can use the below code:

'my_disk' => [
    'driver' => 'local',
    'root' => storage_path('app/my_disk'),
],

 

Step 3 - Save the changes to the filesystems.php file.

 

Step 4 - You can now use the Storage facade to interact with your new disk.

For example-

To store a file on my_disk, you can use the below code:

use Illuminate\Support\Facades\Storage;

Storage::disk('my_disk')->put('file.txt', 'Hello, World!');

 

This will store the file.txt file with the content Hello, World! in storage/app/my_disk. You can use various other methods of the Storage facade to work with files on your new disk, such as get(), exists(), delete(), etc.

 

Note: you can use other drivers for your disk, such as s3, rackspace, ftp, etc. Just update the driver value in your disk configuration accordingly.

Comments 0

Leave a comment