Data & Storage

Storage

Lunaris ships a tiny filesystem abstraction: the Storage facade maps named disks to pluggable drivers, so reading, writing and serving files never touches a raw path.

Disks

Two local disks are registered at boot from config("storage.disks.*"). Each disk has a driver and a root directory:

DiskDriverRootPurpose
privateLocalDriverstorage/app (storage_path("app"))Application files that are never served directly.
publicLocalDriverpublic/storage (public_path("storage"))Files intended to be web-accessible.

Disks are declared in config/vars.php:

"storage" => [
    "disks" => [
        "private" => [
            "driver" => "private",
            "root"   => storage_path("app")
        ],
        "public" => [
            "driver" => "public",
            "root"   => public_path("storage")
        ]
    ]
],

Getting a driver

Resolve a disk through the Storage facade — never by building a path yourself:

use System\Support\Storage\Storage;

$disk = Storage::private();        // the private disk
$disk = Storage::public();         // the public disk
$disk = Storage::driver("public"); // by name

A missing driver name throws an InvalidArgumentException.

The StorageDriver contract

Every driver implements System\Support\Storage\StorageDriver:

MethodReturnsDescription
put(string $path, string $contents)boolWrite $contents to $path, creating parent directories.
putUploadedFile(string $dir, array $file, ?string $name = null)string|falseStore a $_FILES entry; returns the stored relative path, or false.
get(string $path)stringRead a file's contents (throws if missing).
exists(string $path)boolTrue if a regular file exists at $path.
delete(string $path)boolDelete a regular file.
move(string $from, string $to)boolMove/rename a file.
copy(string $from, string $to)boolCopy a file.

Using the private disk

use System\Support\Storage\Storage;

$disk = Storage::private();

$disk->put("reports/2026.txt", "hello");       // writes storage/app/reports/2026.txt
$disk->exists("reports/2026.txt");             // true
$contents = $disk->get("reports/2026.txt");    // "hello"
$disk->copy("reports/2026.txt", "reports/2026.bak");
$disk->move("reports/2026.bak", "archive/2026.bak");
$disk->delete("reports/2026.txt");

Handling uploads

putUploadedFile() stores an entry from $_FILES and returns the relative path you can persist (or false on any failure):

$path = Storage::public()->putUploadedFile("avatars", $_FILES["avatar"]);

if ($path === false) {
    return response()->status(422)->json(["error" => "Upload failed"]);
}

// $path is e.g. "avatars/1a2b.png" → served from public/storage/avatars/1a2b.png

The uploaded file name is sanitized — anything but [A-Za-z0-9._-] becomes _ — and the original basename is used when no explicit name is given.

LocalDriver guarantees

System\Support\Storage\Drivers\LocalDriver is the built-in driver, and it is defensive by design:

  • Path containment. Every path is resolved against the disk root and validated with Path::assertContained() — traversal (..), absolute paths, backslashes and null bytes are rejected.
  • No symlinks. Reads, writes, moves, copies and deletes refuse to operate on symbolic links or on files that resolve outside the root.
  • Uploads only. putUploadedFile() returns false unless the entry is a genuine HTTP upload (UPLOAD_ERR_OK + is_uploaded_file()), so it cannot be used with arbitrary local paths.
  • No overwrite. putUploadedFile() refuses to overwrite an existing target.
  • Regular files only. delete() throws for anything that is not a regular file.

Path safety (System\Support\Path)

Path is the guard used by LocalDriver. Given a root directory, it exposes:

MethodDescription
normalize($path)Rejects empty/absolute paths, ../. segments, backslashes and null bytes; returns a safe relative path.
resolve($path)Joins the normalized relative path onto the root.
assertContained($path)Confirms the path stays inside the root and traverses no symlinks (re-evaluates realpath() when the target exists).
ensureDirectory($path)Creates the directory tree (mode 0755) after validating containment.

Part of the Lunaris documentation · Source on Codeberg · MIT License