Skip to main content
Version: 1.x
Features · v1.x

File System

Mantle includes the Flysystem package to power an abstract interface to various local and remote filesystems. Files can exist local on the disk (inside the normal wp-content/uploads folder) or in a S3 bucket. The API remains the same for all drivers, allowing a fluent interface to storing a file in a remote bucket with only a few lines of configuration.

Configuration​

The configuration for the filesystem lives in config/filesystem.php. This file will contain the "disks" that you can access as well as the "disk's" driver. For example, a application can have multiple S3 disks that all use the 's3' driver. The disk can reference a different bucket set of configuration for the same driver.

You may configure as many disks as you like, and may even have multiple disks that use the same driver.

Local Storage​

By default, the application will use the local storage disk/driver. This will store files in WordPress' upload directory. Storage of files here will always be assumed to be public.

Storage::put( 'file.txt', 'Contents' );
Storage::drive( 'local' )->put( 'file.txt', 'Contents' );

Remote Storage​

Out of the box, Mantle supports the local and s3 drivers.

Composer Dependency​

Before using the S3 driver, you will need to install the Flysystem S3 adapter via Composer:

composer require league/flysystem-aws-s3-v3 "^3.0"

Configuration​

The S3 configuration is located in the config/filesystem.php file. To use the S3 disk you need to fill in your own S3 configuration and credentials.

Obtaining Disk Instances​

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

use Mantle\Facade\Storage;

Storage::put( 'avatars/1', $file_contents );

If your application interacts with multiple disks, you may use the disk method on the Storage facade to work with files on a particular disk:

Storage::drive( 's3' )->put( 'avatars/1', $file_contents );

Storing Files​

Files can be stored using the put() method by passing either file contents or a file stream.

use Mantle\Facade\Storage;

Storage::put( 'avatars/1', $file_contents );

You can upload a local file with the put_file() method. Use the put_file_as to specify a name.

use Mantle\Facade\Storage;

Storage::put_file( '/path/to/store', '/var/local.jpg' );

File Uploads​

Files can be uploaded directly from HTTP requests to any storage disk.

namespace App\Http\Controller;

use Mantle\Http\Controller;
use Mantle\Http\Request;

class Photo_Controller extends Controller {
public function __invoke( Request $request ) {
$path = $request->file( 'avatar' )->store( 'avatars', 'disk-name' );

return $path;
}
}

The first argument to store() is the directory to store the file in and the second is the name of the disk (or an array of options). The file is given a unique, hashed name. Use store_as() to specify the file name, or store_publicly() / store_publicly_as() to store it with public visibility.

The uploaded file can also be stored and added to the WordPress media library with store_as_attachment(), which returns the Attachment model:

$attachment = $request->file( 'avatar' )->store_as_attachment( 'avatars' );

You can also use the Storage facade directly.

use Mantle\Facade\Storage;

Storage::put_file( 'avatars', $request->file( 'avatar' ) );

File Visibility​

By default files will assume to be stored publicly. All local files will be stored publicly out of the box, too. You can use a remote disk such as S3 to store a file privately.

tip

To add a private local disk, add a disk and specify a path to a private folder.

use Mantle\Facade\Storage;

Storage::put( 'file.jpg', $contents, 'public' );

You can store a private file by changing the third argument.

use Mantle\Facade\Storage;

Storage::drive( 's3' )->put( 'file.jpg', $contents, 'private' );
Storage::drive( 's3' )->put( 'file.jpg', $contents, [ 'visibility' => 'private' ] );

Retrieving Files​

Files can be retrieved using the Storage facade.

use Mantle\Facade\Storage;

$path = Storage::url( '/path/to/file.jpg' );

Retrieving Private Files​

Private files commonly need to use a temporary URL to retrieve the file. For a private file on S3, Mantle can generate a temporary S3 URL that expires at the given time.

use Mantle\Facade\Storage;

$url = Storage::drive( 's3' )->temporary_url( '/path/to/file.jpg', now()->addMinutes( 5 ) );