Storage

The Storage class is Slenix's filesystem abstraction layer. It provides a simple, unified API for reading, writing, and managing files across multiple storage disks, without dealing with absolute paths or directory creation manually.


Disks

A disk is a named root directory where files are stored. Slenix comes with four built-in disks:

DiskPathBrowser accessible
publicstorage/app/publicYes — via public/storage/
localstorage/app/privateNo
logsstorage/logsNo
cachestorage/cacheNo

The default disk is public. All static methods on Storage operate on this disk unless you switch with Storage::disk().

Files on the public disk are intended to be served by the web server via a symlink or copy at public/storage/. Files on local are never reachable from a browser.


Switching Disks

Storage::disk(string $disk)

Returns a StorageDisk instance scoped to the given disk. Chain any file operation on it.

php
// Write to the private disk
Storage::disk('local')->put('exports/report.csv', $csv);

// Read from the logs disk
$log = Storage::disk('logs')->get('app.log');

Changing the default disk

php
Storage::setDefaultDisk('local');

// All subsequent static calls now operate on 'local'
Storage::put('data.json', $json);

Registering a custom disk

php
Storage::setDisk('backups', '/mnt/backups');

Storage::disk('backups')->put('db_2025.sql', $dump);

Writing Files

put(string $path, mixed $contents): bool

Writes a string or resource stream to a file. Creates intermediate directories automatically.

php
// Write a string
Storage::put('images/avatar.jpg', $binaryData);

// Write from a resource
$stream = fopen('/tmp/upload.tmp', 'rb');
Storage::disk('local')->put('exports/data.csv', $stream);

Reading Files

get(string $path): string|false

Returns the file contents as a string, or false if the file does not exist.

php
$contents = Storage::get('documents/terms.txt');

if ($contents === false) {
    // file not found
}

Checking Existence

exists(string $path): bool

php
if (Storage::exists('avatars/user_42.jpg')) {
    // file is present
}

missing(string $path): bool (StorageDisk only)

Inverse of exists().

php
if (Storage::disk('local')->missing('cache/config.php')) {
    // regenerate cache
}

Deleting Files

delete(string $path): bool

php
Storage::delete('temp/old_export.zip');

URLs

url(string $path): string

Generates the public HTTP URL for a file. Only available on the public disk. Calling it on a private disk throws a RuntimeException.

php
$url = Storage::url('avatars/user_42.jpg');
// → https://myapp.com/storage/avatars/user_42.jpg

The base URL is resolved from the APP_BASE_URL environment variable.


File Information

path(string $path): string

Returns the absolute filesystem path of a file.

php
$absolute = Storage::path('reports/q3.pdf');
// → /var/www/myapp/storage/app/public/reports/q3.pdf

size(string $path): int

Returns the file size in bytes. Returns 0 if the file does not exist.

php
$bytes = Storage::disk('local')->size('exports/data.csv');

lastModified(string $path): int (StorageDisk only)

Returns the Unix timestamp of the last modification.

php
$timestamp = Storage::disk('local')->lastModified('cache/config.php');
echo date('Y-m-d H:i', $timestamp);

mimeType(string $path): string (StorageDisk only)

Detects the MIME type of a file.

php
$mime = Storage::disk('public')->mimeType('uploads/photo.jpg');
// → image/jpeg

Copying and Moving

copy(string $from, string $to): bool

Copies a file within the same disk.

php
Storage::copy('avatars/default.jpg', 'avatars/user_99.jpg');

move(string $from, string $to): bool

Moves (renames) a file within the same disk.

php
Storage::move('temp/upload_abc.jpg', 'avatars/user_42.jpg');

Directories

makeDirectory(string $path): bool

Creates a directory recursively.

php
Storage::makeDirectory('exports/2025/q3');

files(?string $directory = null): array

Lists files in a directory (non-recursive). Returns absolute paths.

php
$files = Storage::files('avatars');
// → ['/storage/app/public/avatars/user_1.jpg', ...]

allFiles(?string $directory = null): array (StorageDisk only)

Lists all files in a directory recursively.

php
$all = Storage::disk('local')->allFiles('exports');

directories(?string $directory = null): array (StorageDisk only)

Lists subdirectories in a given path.

php
$dirs = Storage::disk('local')->directories('exports');

deleteDirectory(string $path): bool (StorageDisk only)

Deletes a directory and all its contents recursively.

php
Storage::disk('local')->deleteDirectory('temp');

Method Reference

Storage — Static API (default disk)

MethodReturnsDescription
Storage::disk($disk)StorageDiskReturns a disk instance
Storage::put($path, $contents)boolWrites a file
Storage::get($path)`string\false`Reads a file
Storage::exists($path)boolChecks if a file exists
Storage::delete($path)boolDeletes a file
Storage::url($path)stringPublic URL (public disk only)
Storage::path($path)stringAbsolute filesystem path
Storage::files($directory)arrayLists files in a directory
Storage::makeDirectory($path)boolCreates a directory
Storage::size($path)intFile size in bytes
Storage::copy($from, $to)boolCopies a file
Storage::move($from, $to)boolMoves a file
Storage::setDefaultDisk($disk)voidChanges the default disk
Storage::setDisk($name, $path)voidRegisters a custom disk

StorageDisk — Instance API

All methods above are available on a disk instance, plus:

MethodReturnsDescription
->missing($path)boolInverse of exists()
->lastModified($path)intUnix timestamp of last modification
->mimeType($path)stringDetected MIME type
->allFiles($directory)arrayRecursive file listing
->directories($directory)arrayLists subdirectories
->deleteDirectory($path)boolDeletes a directory recursively

Complete Example

php
use Slenix\Supports\Storage\Storage;

public function uploadAvatar(Request $request): void
{
    $file = $request->file('avatar');

    if (!$file->isValid()) {
        return response()->json(['error' => 'Invalid file.'], 422);
    }

    // Build a unique path
    $path = 'avatars/' . uniqid() . '.' . $file->extension();

    // Store on the public disk
    Storage::put($path, file_get_contents($file->tmpPath()));

    // Get the public URL to save in the database
    $url = Storage::url($path);

    User::find($request->input('user_id'))->update(['avatar' => $url]);

    return response()->json(['avatar' => $url]);
}
php
// Clean up temporary exports older than 24 hours
$files = Storage::disk('local')->allFiles('exports');

foreach ($files as $file) {
    $relative = str_replace(Storage::disk('local')->path(''), '', $file);

    if (Storage::disk('local')->lastModified($relative) < time() - 86400) {
        Storage::disk('local')->delete($relative);
    }
}