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 to use drivers for working with local filesystems and Amazon S3. Even better, it's amazingly simple to switch between these storage options as the API remains the same for each system.
Configuration
The filesystem configuration file is located at config/filesystems.php
. Within this file you may configure all of your "disks". Each disk represents a particular storage driver and storage location. Example configurations for each supported driver are included in the configuration file. So, modify the configuration to reflect your storage preferences and credentials.
You may configure as many disks as you like, and may even have multiple disks that use the same driver.
The Public Disk
The public
disk is intended for files that are going to be publicly accessible. By default, the public
disk uses the local
driver and stores these files in storage/app/public
. To make them accessible from the web, you should create a symbolic link from public/storage
to storage/app/public
. This 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');
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 store a file in storage/app/file.txt
:
Storage::disk('local')->put('file.txt', 'Contents');
Permissions
The public
visibility translates to 0755
for directories and 0644
for files. You can modify the permissions mappings in your filesystems
configuration file:
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'permissions' => [
'file' => [
'public' => 0664,
'private' => 0600,
],
'dir' => [
'public' => 0775,
'private' => 0700,
],
],
],