⬡ Lacelium
⌘K
Version 2.x ▾

File Storage

Introduction

Lacelium provides a powerful filesystem abstraction based on the popular Flysystem PHP package. The filesystem integration provides simple drivers for working with local filesystems, public asset directories, and cloud-based storage services such as Amazon S3, Cloudflare R2, or MinIO.

Configuration

Your application's filesystem configuration file is located at config/filesystems.php. Within this file, you may configure all of your filesystem "disks". Each disk represents a particular storage driver and storage location.

// config/filesystems.php
return [
    'default' => env('FILESYSTEM_DISK', 'local'),

    'disks' => [
        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
            'throw' => false,
        ],

        'public' => [
            'driver' => 'local',
            'root' => public_path('uploads'),
            'url' => env('APP_URL') . '/uploads',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
        ],
    ],
];

Obtaining Disk Instances

You may interact with any configured disk using the global storage() helper function:

use Lacelium\Core\Storage;

// Interact with the default disk
storage()->put('report.csv', $csvContent);

// Interact with a specific disk
storage('public')->put('avatars/user_1.png', $imageBinary);
storage('s3')->put('backups/db.sql.gz', $backup);

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:

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

Checking File Existence

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

if (storage()->exists('file.jpg')) {
    // File exists
}

File URLs

You can use the url method to get the publicly accessible URL for a given file stored on the public or s3 disk:

$url = storage('public')->url('avatars/avatar.png');
// http://localhost:8000/uploads/avatars/avatar.png

Storing Uploaded Files

In web applications, one of the most common use cases for storing files is storing user-uploaded files, such as photos, documents, and invoices. Lacelium makes it very easy to store uploaded files using the global fileUpload() helper:

// Handling a Symfony / HTTP request file upload
$file = $request->files->get('avatar');

if ($file) {
    // Sanitizes file name, validates MIME type, and moves to target directory
    $storedFilename = fileUpload($file, 'uploads/avatars', [
        'allowed' => ['image/jpeg', 'image/png', 'image/webp'],
        'max_size' => 5 * 1024 * 1024, // 5MB
    ]);
}

Deleting Files

The delete method accepts a single filename or an array of files to remove from the disk:

storage()->delete('file.jpg');

storage()->delete(['file_1.jpg', 'file_2.jpg']);

Next Steps