File Storage in Laravel provides an abstraction layer for managing files across local, cloud, or remote storage systems. It uses the Flysystem library to support various storage drivers like Amazon S3, FTP, and local storage.
Laravel's file storage system leverages Flysystem, a powerful PHP library for handling file storage, introduced to simplify working with different storage backends using a unified API.
.env
for better security and flexibility.Configure storage in config/filesystems.php
:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
],
],
Store a file:
use Illuminate\Support\Facades\Storage;
Storage::disk('local')->put('example.txt', 'File contents here');
Retrieve a file URL:
$url = Storage::disk('s3')->url('example.txt');
Laravel's file storage system provides a powerful and consistent way to handle files across various storage solutions.