File Storage
Introduction
Laravel provides a powerful filesystem abstraction thanks to the wonderful Flysystem PHP package by Frank de Jonge. The Laravel Flysystem integration provides simple drivers for working with local filesystems, SFTP, and Amazon S3. Even better, it's amazingly simple to switch between these storage options between your local development machine and production server as the API remains the same for each system.
Configuration
Laravel's filesystem configuration file is located at config/filesystems.php
. Within this file, you may configure all of your filesystem "disks". Each disk represents a particular storage driver and storage location. Example configurations for each supported driver are included in the configuration file so you can modify the configuration to reflect your storage preferences and credentials.
The local
driver interacts with files stored locally on the server running the Laravel application while the s3
driver is used to write to Amazon's S3 cloud storage service.
You may configure as many disks as you like and may even have multiple disks that use the same driver.
The Local Driver
When using the local
driver, all file operations are relative to the root
directory defined in your filesystems
configuration file. By default, this value is set to the storage/app
directory. Therefore, the following method would write to storage/app/example.txt
:
use Illuminate\Support\Facades\Storage;
Storage::disk('local')->put('example.txt', 'Contents');
The Public Disk
The public
disk included in your application's filesystems
configuration file is intended for files that are going to be publicly accessible. By default, the public
disk uses the local
driver and stores its files in storage/app/public
.
To make these files accessible from the web, you should create a symbolic link from public/storage
to storage/app/public
. Utilizing this folder convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.
To create the symbolic link, you may use the storage:link
Artisan command:
php artisan storage:link
Once a file has been stored and the symbolic link has been created, you can create a URL to the files using the asset
helper:
echo asset('storage/file.txt');
You may configure additional symbolic links in your filesystems
configuration file. Each of the configured links will be created when you run the storage:link
command:
'links' => [
public_path('storage') => storage_path('app/public'),
public_path('images') => storage_path('app/images'),
],
The storage:unlink
command may be used to destroy your configured symbolic links:
php artisan storage:unlink
Driver Prerequisites
S3 Driver Configuration
Before using the S3 driver, you will need to install the Flysystem S3 package via the Composer package manager:
composer require league/flysystem-aws-s3-v3 "^3.0" --with-all-dependencies
An S3 disk configuration array is located in your config/filesystems.php
configuration file. Typically, you should configure your S3 information and credentials using the following environment variables which are referenced by the config/filesystems.php
configuration file:
AWS_ACCESS_KEY_ID=<your-key-id>
AWS_SECRET_ACCESS_KEY=<your-secret-access-key>
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=<your-bucket-name>
AWS_USE_PATH_STYLE_ENDPOINT=false
For convenience, these environment variables match the naming convention used by the AWS CLI.
FTP Driver Configuration
Before using the FTP driver, you will need to install the Flysystem FTP package via the Composer package manager:
composer require league/flysystem-ftp "^3.0"
Laravel's Flysystem integrations work great with FTP; however, a sample configuration is not included with the framework's default config/filesystems.php
configuration file. If you need to configure an FTP filesystem, you may use the configuration example below:
'ftp' => [
'driver' => 'ftp',
'host' => env('FTP_HOST'),
'username' => env('FTP_USERNAME'),
'password' => env('FTP_PASSWORD'),
// Optional FTP Settings...
// 'port' => env('FTP_PORT', 21),
// 'root' => env('FTP_ROOT'),
// 'passive' => true,
// 'ssl' => true,
// 'timeout' => 30,
],
SFTP Driver Configuration
Before using the SFTP driver, you will need to install the Flysystem SFTP package via the Composer package manager:
composer require league/flysystem-sftp-v3 "^3.0"
Laravel's Flysystem integrations work great with SFTP; however, a sample configuration is not included with the framework's default config/filesystems.php
configuration file. If you need to configure an SFTP filesystem, you may use the configuration example below:
'sftp' => [
'driver' => 'sftp',
'host' => env('SFTP_HOST'),
// Settings for basic authentication...
'username' => env('SFTP_USERNAME'),
'password' => env('SFTP_PASSWORD'),
// Settings for SSH key based authentication with encryption password...
'privateKey' => env('SFTP_PRIVATE_KEY'),
'passphrase' => env('SFTP_PASSPHRASE'),
// Settings for file / directory permissions...
'visibility' => 'private', // `private` = 0600, `public` = 0644
'directory_visibility' => 'private', // `private` = 0700, `public` = 0755
// Optional SFTP Settings...
// 'hostFingerprint' => env('SFTP_HOST_FINGERPRINT'),
// 'maxTries' => 4,
// 'passphrase' => env('SFTP_PASSPHRASE'),
// 'port' => env('SFTP_PORT', 22),
// 'root' => env('SFTP_ROOT', ''),
// 'timeout' => 30,
// 'useAgent' => true,
],
Scoped and Read-Only Filesystems
Scoped disks allow you to define a filesystem where all paths are automatically prefixed with a given path prefix. Before creating a scoped filesystem disk, you will need to install an additional Flysystem package via the Composer package manager:
composer require league/flysystem-path-prefixing "^3.0"
You may create a path scoped instance of any existing filesystem disk by defining a disk that utilizes the scoped
driver. For example, you may create a disk which scopes your existing s3
disk to a specific path prefix, and then every file operation using your scoped disk will utilize the specified prefix:
's3-videos' => [
'driver' => 'scoped',
'disk' => 's3',
'prefix' => 'path/to/videos',
],
"Read-only" disks allow you to create filesystem disks that do not allow write operations. Before using the read-only
configuration option, you will need to install an additional Flysystem package via the Composer package manager:
composer require league/flysystem-read-only "^3.0"