When working with PHP projects you sooner or later need to interact with files. This can be done in a variety of ways, and PHP offers a few good built-in features like the “file_get_contents” and “file_put_content” functions. However, you may need more advanced features – and may need to interact with other file systems than your local harddrive. For that purpose the Laravel framework comes with built in storage handling which is compatible with a variety of systems. It is built upon the popular library Flysystem and therefore integrates with lots of services via the same common interface.
Storage configuration resides in the “config/filesystems.php” file. You can add multiple engines such as local drives and cloud drives.
Basic file operations
The filesystem integration provides a straight forward way of interacting with files. Basic write/read operations are illustrated below.
// Write content to file $content = 'File content ...'; Storage::put( 'my_file.txt', $content ); // Read content from file $content = Storage::get( 'my_file.txt' ); // Delete file Storage::delete( 'my_file.txt' );
You can also append/prepend content easily to existing files.
// Append content to file $append = 'Appended text...'; Storage::append( 'my_file.txt', $append ); // Prepend content to file $prepend = 'Prepended text...'; Storage::prepend( 'my_file.txt', $prepend );
Managing files and directories
You can also easily work with multiple leves of files by using directories.
$directory = '/path/to/my/dir'; // Create a directory Storage::makeDirectory($directory); // Delete a directory Storage::deleteDirectory($directory); // Copy file somewhere else Storage::copy('my_file.txt', 'my_file_copy.txt'); // Move file somewhere else Storage::move('old/my_file.txt', 'new/my_file.txt');
Utility functions
The utility functions allow to query meta data about files.
// Does a file exist? $exists = Storage::exists('my_file.txt'); // What is the size of a file? $size = Storage::size('my_file.txt'); // What time was the file last modified? $time = Storage::lastModified('my_file.txt');
Listing files and directories
Sometimes you need to read your file structure which can be done with the listing methods. You can list both files and directories in one level or recursively.
// List all files in directory $files = Storage::files($directory); // List all files & directories in directory $files = Storage::allFiles($directory); // List all directories in directory $directories = Storage::directories($directory); // List all directories recursively in directory $directories = Storage::allDirectories($directory);
Working with files does not have to be complicated as you can see above. Again the Laravel framework comes with tools in hand for you to build your project quickly. Another smart thing about the Flysystem is that it emulates directory structures in systems that do not normally support them, such as for example the Amazon S3 storage service. Providing a standardized interface allows you to integrate and switch between different storage engines seamlessly, which is a great benefit during testing and deployment.
No comments yet (leave a comment)