File System

PHP File System Helpers

Small helpers for private directories and bounded text-file reads.

Overview

Small helpers for private directories and bounded text-file reads.

Code example

<?php
declare(strict_types=1);
function ensurePrivateDirectory(string $path): void {
    if (!is_dir($path) && !mkdir($path,0700,true) && !is_dir($path)) throw new RuntimeException('Cannot create directory.');
}
function readTextFile(string $path,int $maxBytes=1048576): string {
    if (!is_file($path) || filesize($path)>$maxBytes) throw new RuntimeException('Missing or oversized file.');
    $v=file_get_contents($path); if($v===false) throw new RuntimeException('Read failed.'); return $v;
}

How to use it

Place the snippet in a namespaced helper or service file, adapt its inputs to your application and add tests for expected and edge-case behavior.

Notes

The example is intentionally small. Validate untrusted input, handle exceptions and document any PHP extension or database requirements.