Filesystem configuration helps your Laravel app save and get files easily. It tells Laravel where and how to store files.
Filesystem configuration in 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.
'default' => 'local',
'disks' => [ 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'visibility' => 'public', ], ],
'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'), ], ],
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.
<?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'; } }
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.
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.
Practice
default key in Laravel's config/filesystems.php file?Solution
Step 1: Understand the role of the
Thedefaultkeydefaultkey inconfig/filesystems.phptells Laravel which disk to use when no specific disk is mentioned.Step 2: Compare with other options
Options A, B, and C describe unrelated settings. Only "It sets the default storage disk Laravel uses for file operations." correctly describes the default disk setting.Final Answer:
It sets the default storage disk Laravel uses for file operations. -> Option AQuick Check:
Default disk = storage location [OK]
- Confusing default disk with file size limits
- Thinking default sets encryption
- Assuming default lists all disks
config/filesystems.php?Solution
Step 1: Identify the correct driver for local disk
The local disk uses the 'local' driver, so options with 'ftp' or 's3' are incorrect.Step 2: Check the root path for local storage
The root path for local disk is usuallystorage_path('app'), notpublic_path('app').Final Answer:
'local' => ['driver' => 'local', 'root' => storage_path('app')], -> Option BQuick Check:
Local driver + storage_path = correct syntax [OK]
- Using wrong driver like 'ftp' or 's3' for local disk
- Setting root to public_path instead of storage_path
- Mixing driver and root paths
config/filesystems.php snippet:'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'),
],
],
'default' => 's3',What happens when you call
Storage::put('file.txt', 'Hello') in your app?Solution
Step 1: Identify the default disk
The default disk is set to 's3', so all Storage calls use the S3 disk unless specified otherwise.Step 2: Understand Storage::put behavior
CallingStorage::putsaves the file to the default disk, which is the S3 bucket configured.Final Answer:
The file is saved to the configured Amazon S3 bucket. -> Option CQuick Check:
Default disk 's3' means files go to S3 [OK]
- Assuming file saves locally despite default 's3'
- Thinking missing local disk causes error
- Confusing temporary directory with storage
config/filesystems.php but get an error when using it. Which of these is the most likely cause?Solution
Step 1: Understand Laravel config caching
Laravel caches config files for performance. Changes in config files require clearing or caching again.Step 2: Identify the common fix
Runningphp artisan config:cacherefreshes the cached config and applies new disk settings.Final Answer:
You forgot to run php artisan config:cache after changing the config. -> Option AQuick Check:
Config cache must refresh after edits [OK]
- Ignoring config cache after edits
- Blaming PHP version for config errors
- Confusing unrelated packages with filesystem config
config/filesystems.php that stores files on a remote FTP server. Which of these is the correct minimal configuration?Solution
Step 1: Identify the correct driver for FTP
The driver must be 'ftp' to connect to an FTP server, so options with 'local' or 's3' are incorrect.Step 2: Check required connection details
FTP requires host, username, and password. 'ftp' => [ 'driver' => 'ftp', 'host' => env('FTP_HOST'), 'username' => env('FTP_USERNAME'), 'password' => env('FTP_PASSWORD') ], includes these minimal required keys.Final Answer:
'ftp' => [ 'driver' => 'ftp', 'host' => env('FTP_HOST'), 'username' => env('FTP_USERNAME'), 'password' => env('FTP_PASSWORD') ], -> Option DQuick Check:
FTP driver + host, username, password needed [OK]
- Using wrong driver like 'local' or 's3' for FTP
- Missing required host or credentials
- Confusing bucket with FTP config
Ready to prove it?
Pass the test and this pattern moves unknown
