Laravel 5 includes the excellent Flysystem project for interacting with files both in the application filesystem, as well as popular cloud-based storage solutions such as Amazon Simple Storage Service (Amazon S3) and Rackspace. Filesystems are configured as disks in the config/filesystems.php
file. You can then use a consistent API to manage files, whether they are located locally or in an external cloud store.
Calling methods directly on the Storage
façade will call those methods on the default disk as follows:
Storage::exists('foo.txt');
You can also explicitly specify the disk to perform actions on, in case you have more than one disk configured, as follows:
Storage::disk('local')->exists('foo.txt');
You can read and write data to files as follows:
Storage::put('foo.txt', $contents); $contents = Storage::get('foo.txt');
You can also prepend or append data instead as follows:
Storage::prepend('foo.txt', 'Text to prepend.'); Storage::append('foo.txt', 'Text to append.');
You can copy and move files with the aptly-named methods as follows:
Storage::copy($source, $destination); Storage::move($source, $destination);
And you can also delete files, either one at a time or multiple files in one go, by supplying an array of files to delete, as shown in the following code:
Storage::delete('foo.txt'); Storage::delete(['foo.txt', 'bar.txt']);
There are also various other helpful methods that allow you to retrieve useful information about a file as follows:
Storage::size('foo.txt'); Storage::lastModified('foo.txt');
Apart from working with files, you can work with directories. To list all files within a particular directory use the following code:
Storage::files('path/to/directory');
The preceding code will only list files in the current directory. If you wanted to list all files recursively (that is, files in the current directory and any subdirectories), then you can use the allFiles
method as follows:
Storage::allFiles('path/to/directory');
You can create directories as follows:
Storage::makeDirectory('path/to/directory');
And you can also delete directories as follows:
Storage::deleteDirectory('path/to/directory');
Handling file uploads is easy in Laravel 5. The first step is to create a form that will send files when submitted:
{!! Form::open(['files' => true) !!}
This will set the enctype
attribute to multipart/form-data
. You then need an HTML file
input:
{!! Form::file('avatar') !!}
On submission, you can access the file from the Request
object in your controller actions as follows:
public function store(Request $request){$file = $request->file('avatar');}
From here, you will normally move the file to a directory of your choice:
public function store(Request $request)
{
$file = $request->file('avatar');
$file->move(storage_path('uploads/avatars'));
}
In the preceding example, $file
is an instance of the Symfony\Component\HttpFoundation\File\UploadedFile
class, which provides a number of handy methods for interacting with the uploaded file.
You can get the full path to the file as follows:
$path = $request->file('avatar')->getRealPath();
You can get the name of the file as uploaded by the user as follows:
$name = $request->file('avatar')->getClientOriginalName();
You can also retrieve just the extension of the original file as follows:
$ext = $request->file('avatar')->getClientOriginalExtension();