Zap
Documentation / Utilities

File

Use Zap's File utility for public media URLs and controlled file reads, writes, uploads, listings, deletions, and downloads. File access is restricted to the roots configured by your application.

Build a URL for public media

Store publicly served files under public/media. In a view, use the get_media() helper to build a URL; it verifies that the asset exists within that directory and returns the framework's media-not-found image otherwise.

<img src="{{ get_media('images/logo.svg') }}" alt="Company logo">
Do not put private uploads in public/media. Use a protected storage directory and serve those files through an authorized download action instead.

Read, write, and append

The static methods accept paths relative to the application root. Writes create parent directories when needed. Each operation returns an array with an error status and a human-readable message; check the status before using any returned data.

By default, config/app.php limits these operations to app.allowed_roots, which includes storage/ and public/media/. Add only directories the application genuinely needs.

use Zap\Core\Utils\File;

$write = File::put('storage/reports/status.txt', 'Report ready');
if ($write['error'] !== 0) {
 // Handle the write failure.
}

$append = File::append('storage/reports/status.txt', 'Next update');
$read = File::get('storage/reports/status.txt');
if ($read['error'] === 0) {
 $contents = $read['data'];
}

File::append() adds a line break before new data when the existing file is non-empty. File::get() returns the file contents in data after a successful read.

List and delete files

list_files() lists files directly inside an allowed directory; it does not recursively walk subdirectories. delete_file() removes one existing file and reports success or failure in its result array.

$listing = File::list_files('storage/reports');
if ($listing['error'] === 0) {
 foreach ($listing['data'] as $item) {
 echo $item['name'];
 }
}

$result = File::delete_file('storage/reports/old-report.txt');
Never pass an unchecked user-supplied path to a file operation. Resolve the file from an application-owned identifier, then authorize the current user before reading or deleting it.

Accept an upload

Resolve File from the container, configure the destination and limits, then pass the form field name to do_upload(). The default upload destination is storage/; successful uploads get a random filename by default.

use Zap\Core\Utils\File;

$file = app(File::class);
$file->init_upload([
 'upload_path' => BASE_PATH . '/storage/uploads',
 'allowed_types' => ['jpg', 'png', 'pdf'],
 'max_size' => 2048, // KB
 'encrypt_name' => true,
]);

// assume that the file input field name is document

if (!$file->do_upload('document')) {
 $errors = $file->get_upload_errors();
 return $this->text($errors['error'] ?? 'Upload failed.', 422);
}

$uploadedFile = $file->get_upload_data();

The form must use method="post" and enctype="multipart/form-data". Uploads are checked for their HTTP upload source, size, allowed extension, and matching MIME type. Keep request-token validation and authorization in the controller action before processing an upload.

Serve a protected download

File::download() streams a file and exits the request. Configure app.downloadable_roots with the directories from which downloads may be served; by default, this is storage/. Pass an optional safe display name as the second argument.

use Zap\Core\Utils\File;

public function downloadReport(string $reportId): void
{
 // Find the report using an application-owned ID and authorize access first.
 File::download('storage/reports/' . $reportId . '.pdf', 'report.pdf');
}
Do not concatenate an unvalidated request path into a download path. Look up a server-side record, confirm the requester may access it, then use its stored path.
Next: send email

Continue with the Email utility guide.

Email utility