How to add files to disk on laravel?

by mallory_cormier , in category: PHP Frameworks , 7 months ago

How to add files to disk on laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 7 months ago

@mallory_cormier 

To add files to disk on Laravel, you can follow these steps:

  1. Define a disk in the config/filesystems.php file. You can define a new disk by adding an entry to the disks array. For example:
1
2
3
4
5
6
'disks' => [
    'my_disk' => [
        'driver' => 'local',
        'root' => storage_path('app/my_disk'),
    ],
],


  1. Use the Storage facade in your code to interact with the disk. For example, to store a file on the disk you defined above, you can use:
1
Storage::disk('my_disk')->put('filename.txt', 'File contents');


You can also store a file from a form input or request like:

1
Storage::disk('my_disk')->put('filename.txt', $request->file('file'));


  1. Retrieve files from the disk by using the get method. For example:
1
$fileContents = Storage::disk('my_disk')->get('filename.txt');


  1. Remember to run the php artisan storage:link command to create a symbolic link from the public/storage directory to the storage/app/public directory. This will make it possible to retrieve files from the disk in a web browser.


By following these steps, you can easily add files to disk on Laravel and interact with them in your application.