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:
| Disk | Path | Browser accessible |
|---|---|---|
public | storage/app/public | Yes — via public/storage/ |
local | storage/app/private | No |
logs | storage/logs | No |
cache | storage/cache | No |
The default disk is public. All static methods on Storage operate on this disk unless you switch with Storage::disk().
Files on the
publicdisk are intended to be served by the web server via a symlink or copy atpublic/storage/. Files onlocalare 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.
// 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
Storage::setDefaultDisk('local');
// All subsequent static calls now operate on 'local'
Storage::put('data.json', $json);Registering a custom disk
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.
// 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.
$contents = Storage::get('documents/terms.txt');
if ($contents === false) {
// file not found
}Checking Existence
exists(string $path): bool
if (Storage::exists('avatars/user_42.jpg')) {
// file is present
}missing(string $path): bool (StorageDisk only)
Inverse of exists().
if (Storage::disk('local')->missing('cache/config.php')) {
// regenerate cache
}Deleting Files
delete(string $path): bool
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.
$url = Storage::url('avatars/user_42.jpg');
// → https://myapp.com/storage/avatars/user_42.jpgThe base URL is resolved from the
APP_BASE_URLenvironment variable.
File Information
path(string $path): string
Returns the absolute filesystem path of a file.
$absolute = Storage::path('reports/q3.pdf');
// → /var/www/myapp/storage/app/public/reports/q3.pdfsize(string $path): int
Returns the file size in bytes. Returns 0 if the file does not exist.
$bytes = Storage::disk('local')->size('exports/data.csv');lastModified(string $path): int (StorageDisk only)
Returns the Unix timestamp of the last modification.
$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.
$mime = Storage::disk('public')->mimeType('uploads/photo.jpg');
// → image/jpegCopying and Moving
copy(string $from, string $to): bool
Copies a file within the same disk.
Storage::copy('avatars/default.jpg', 'avatars/user_99.jpg');move(string $from, string $to): bool
Moves (renames) a file within the same disk.
Storage::move('temp/upload_abc.jpg', 'avatars/user_42.jpg');Directories
makeDirectory(string $path): bool
Creates a directory recursively.
Storage::makeDirectory('exports/2025/q3');files(?string $directory = null): array
Lists files in a directory (non-recursive). Returns absolute paths.
$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.
$all = Storage::disk('local')->allFiles('exports');directories(?string $directory = null): array (StorageDisk only)
Lists subdirectories in a given path.
$dirs = Storage::disk('local')->directories('exports');deleteDirectory(string $path): bool (StorageDisk only)
Deletes a directory and all its contents recursively.
Storage::disk('local')->deleteDirectory('temp');Method Reference
Storage — Static API (default disk)
| Method | Returns | Description | |
|---|---|---|---|
Storage::disk($disk) | StorageDisk | Returns a disk instance | |
Storage::put($path, $contents) | bool | Writes a file | |
Storage::get($path) | `string\ | false` | Reads a file |
Storage::exists($path) | bool | Checks if a file exists | |
Storage::delete($path) | bool | Deletes a file | |
Storage::url($path) | string | Public URL (public disk only) | |
Storage::path($path) | string | Absolute filesystem path | |
Storage::files($directory) | array | Lists files in a directory | |
Storage::makeDirectory($path) | bool | Creates a directory | |
Storage::size($path) | int | File size in bytes | |
Storage::copy($from, $to) | bool | Copies a file | |
Storage::move($from, $to) | bool | Moves a file | |
Storage::setDefaultDisk($disk) | void | Changes the default disk | |
Storage::setDisk($name, $path) | void | Registers a custom disk |
StorageDisk — Instance API
All methods above are available on a disk instance, plus:
| Method | Returns | Description |
|---|---|---|
->missing($path) | bool | Inverse of exists() |
->lastModified($path) | int | Unix timestamp of last modification |
->mimeType($path) | string | Detected MIME type |
->allFiles($directory) | array | Recursive file listing |
->directories($directory) | array | Lists subdirectories |
->deleteDirectory($path) | bool | Deletes a directory recursively |
Complete Example
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]);
}// 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);
}
}