Lumen provides a wonderful filesystem abstraction thanks to the Flysystem PHP package by Frank de Jonge. The 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 options are located in your .env
configuration file. You may look at the .env.example
configuration file for an example of using these options.
Before using the S3 or Rackspace drivers, you will need to install the appropriate package via Composer:
- Amazon S3:
league/flysystem-aws-s3-v2 ~1.0
- Rackspace:
league/flysystem-rackspace ~1.0
When using the local
driver, note that all file operations are relative 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');
Note: If you intend to use the
Storage
facade, be sure to uncomment the$app->withFacades()
call in yourbootstrap/app.php
file.
The Storage
facade may be used to interact with any of your configured disks. Alternatively, you may type-hint the Illuminate\Contracts\Filesystem\Factory
contract on any class that is resolved via the Laravel service container.
$disk = Storage::disk('s3');
$disk = Storage::disk('local');
$exists = Storage::disk('s3')->exists('file.jpg');
if (Storage::exists('file.jpg')) {
//
}
$contents = Storage::get('file.jpg');
Storage::put('file.jpg', $contents);
Storage::prepend('file.log', 'Prepended Text');
Storage::append('file.log', 'Appended Text');
Storage::delete('file.jpg');
Storage::delete(['file1.jpg', 'file2.jpg']);
Storage::copy('old/file1.jpg', 'new/file1.jpg');
Storage::move('old/file1.jpg', 'new/file1.jpg');
$size = Storage::size('file1.jpg');
$time = Storage::lastModified('file1.jpg');
$files = Storage::files($directory);
// Recursive...
$files = Storage::allFiles($directory);
$directories = Storage::directories($directory);
// Recursive...
$directories = Storage::allDirectories($directory);
Storage::makeDirectory($directory);
Storage::deleteDirectory($directory);