Filesystem configuration helps your Laravel app save and get files easily. It tells Laravel where and how to store files.
0
0
Filesystem configuration in Laravel
Introduction
When you want to save user-uploaded images or documents.
When you need to store logs or reports on your server or cloud.
When your app needs to read files from different places like local disk or cloud storage.
When you want to switch storage locations without changing your app code.
When you want to organize files in folders or disks for better management.
Syntax
Laravel
return [ 'default' => env('FILESYSTEM_DISK', 'local'), 'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), ], 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ], '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'), ], ], 'links' => [ public_path('storage') => storage_path('app/public'), ], ];
The default key sets which disk Laravel uses if you don't specify one.
Each disk has a driver like 'local' or 's3' to tell Laravel how to handle files.
Examples
This sets the default storage disk to local, which saves files on your server.
Laravel
'default' => 'local',
This configures a public disk where files are accessible via URL.
Laravel
'disks' => [ 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'visibility' => 'public', ], ],
This sets up an Amazon S3 disk to store files in the cloud.
Laravel
'disks' => [ 's3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), ], ],
Sample Program
This controller saves an uploaded photo to the 'public' disk inside a 'photos' folder. It also shows how to get the URL of a saved file.
Laravel
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; class FileController extends Controller { public function upload(Request $request) { $path = $request->file('photo')->store('photos', 'public'); return "File saved at: " . $path; } public function showFile() { if (Storage::disk('public')->exists('photos/example.jpg')) { return Storage::disk('public')->url('photos/example.jpg'); } return 'File not found'; } }
OutputSuccess
Important Notes
Remember to run php artisan storage:link to create a symbolic link for public files.
Use environment variables to keep sensitive info like AWS keys safe.
You can add more disks for different storage needs, like FTP or SFTP.
Summary
Filesystem config tells Laravel where and how to save files.
You can use local storage or cloud services like Amazon S3.
Setting the default disk makes file handling easier in your app.