Filesystem / Cloud Storage

Laravel provides a wonderful filesystem abstraction thanks to the Flysystem PHP package by Frank de Jonge. The Laravel Flysystem integration provides simple to use drivers for working with local filesystems, Amazon S3, and Rackspace Cloud Storage. Even better, it's amazingly simple to switch between these storage options as the API remains the same for each system!

The filesystem configuration file is located at . 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 is included in the configuration file. So, simply modify the configuration to reflect your storage preferences and credentials!

  • Amazon S3: league/flysystem-aws-s3-v2 ~1.0

Of course, you may configure as many disks as you like, and may even have multiple disks that use the same driver.

When using the local driver, note that all file operations are relative to the root directory defined in your 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:

Retrieving A Particular Disk

  1. $disk = Storage::disk('s3');
  2. $disk = Storage::disk('local');

Determining If A File Exists

  1. $exists = Storage::disk('s3')->exists('file.jpg');

Calling Methods On The Default Disk

  1. if (Storage::exists('file.jpg'))
  2. {
  3. //
  4. }

Retrieving A File's Contents

  1. $contents = Storage::get('file.jpg');

Setting A File's Contents

  1. Storage::put('file.jpg', $contents);

Prepend To A File

Append To A File

  1. Storage::append('file.log', 'Appended Text');

Delete A File

  1. Storage::delete('file.jpg');
  2. Storage::delete(['file1.jpg', 'file2.jpg']);

Copy A File To A New Location

  1. Storage::copy('old/file1.jpg', 'new/file1.jpg');

Move A File To A New Location

Get File Size

  1. $size = Storage::size('file1.jpg');

Get The Last Modification Time (UNIX)

Get All Files Within A Directory

  1. $files = Storage::files($directory);
  2. // Recursive...
  3. $files = Storage::allFiles($directory);

Get All Directories Within A Directory

  1. $directories = Storage::directories($directory);
  2. // Recursive...
  3. $directories = Storage::allDirectories($directory);

Create A Directory

  1. Storage::makeDirectory($directory);

Delete A Directory

    Laravel's Flysystem integration provides drivers for several "drivers" out of the box; however, Flysystem is not limited to these and has adapters for many other storage systems. You can create a custom driver if you want to use one of these additional adapters in your Laravel application. Don't worry, it's not too hard!

    In order to set up the custom filesystem you will need to create a service provider such as DropboxFilesystemServiceProvider. In the provider's boot method, you can inject an instance of the Illuminate\Contracts\Filesystem\Factory contract and call the extend method of the injected instance. Alternatively, you may use the Disk facade's extend method.

    Dropbox Example

    1. <?php namespace App\Providers;
    2. use Storage;
    3. use League\Flysystem\Filesystem;
    4. use Dropbox\Client as DropboxClient;
    5. use League\Flysystem\Dropbox\DropboxAdapter;
    6. use Illuminate\Support\ServiceProvider;
    7. class DropboxFilesystemServiceProvider extends ServiceProvider {
    8. public function boot()
    9. {
    10. Storage::extend('dropbox', function($app, $config)
    11. {
    12. $client = new DropboxClient($config['accessToken'], $config['clientIdentifier']);
    13. return new Filesystem(new DropboxAdapter($client));
    14. });
    15. }
    16. public function register()
    17. {
    18. //
    19. }