Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Using files and storage engines with Laravel

Sunday, September 20th, 2015 by Servage

storage-enginesWhen 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

// Delete a 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.

Using files and storage engines with Laravel, 4.0 out of 5 based on 6 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.