Storage

Atom provides a powerful filesystem abstraction. The Atom Filesystem integration provides simple to use drivers for working with local filesystems.

The Public Disk

The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in public/storage.

Of course, once a file has been stored, you can create a URL to the files using the asset helper:

echo asset('storage/file.txt');

The Local Disk

When using the local driver, all file operations are relative to the storage/app directory. Therefore, the following method would store a file in storage/app/file.txt:

$storage = new Atom\Storage\Storage('local');

$storage->put('file.txt', 'Contents');

Obtaining Disk Instances

The Storage class may be used to interact with any of your configured disks. For example, you may use the put method on the class to store an avatar on the default disk. If you call methods on the Storage class without defining the disk argument, the method call will automatically be passed to the local disk:

use Atom\Atom\Storage;

$storage = new Storage('public');
$local_storage = new Storage();

$storage->put('avatar.png', $fileContents');
$local_storage->put('file.txt', 'Contents');

Retrieving Files

The get method may be used to retrieve the contents of a file. The raw string contents of the file will be returned by the method. Remember, all file paths should be specified relative to the "root" location of the disk:

$storage = new Storage('public');

$contents = $storage->get('file.jpg');

The exists method may be used to determine if a file exists on the disk:

$exists = $storage->exists('file.jpg');

File URLs

You may use the path method to get the URL for the given file. If you are using the local driver, this will typically just prepend /storage to the given path and return a relative URL to the file:

$storage = new Storage('public');

$url = $storage->path('file.jpg');

Storing Files

The put method may be used to store raw file contents on a disk. You may also pass a PHP resource to the put method. If you pass a PHP resource as contents, you will need to set the serialize argument to true. Serialize is greatly recommended when dealing with large files:

use Atom\Storage\Storage;

$storage = new Storage('local');

$storage->put('file.txt', 'Simple text contents');

$storage->put('file.bin', $resource, true);

Deleting Files

The delete method accepts a single filename to remove from the disk:

$storage->delete('file.txt');

Get All Files Within A Directory

The getFiles method returns an array of all of the files in the storage disk:

$storage = new Storage('public');

$files = $storage->files();

Dernière mise à jour

Cet article vous a-t-il été utile ?