Framework Docs File & Upload

File & Upload

Two classes handle filesystem work: File provides static helpers for directory and file operations, and Upload validates and moves uploaded files from $_FILES with optional image format conversion.

Namespace: Wojo\Core\File and Wojo\Core\Upload

File — Directory & File Operations

Method Signature Description
deleteFile() static deleteFile(string $file): bool Delete a single file
deleteMulti() static deleteMulti(string $dir): void Delete a file or an entire directory tree
deleteRecursive() static deleteRecursive(string $dir, bool $removeParent = false): true Recursively delete directory contents; optionally remove parent
deleteDirectory() static deleteDirectory(string $dir): bool Empty and remove a directory
emptyDirectory() static emptyDirectory(string $dir): true Delete all files inside a directory (non-recursive)
makeDirectory() static makeDirectory(string $dir): void Create a directory (0755) if it doesn't exist
renameDirectory() static renameDirectory(string $old, string $new): void Rename or move a directory
copyDirectory() static copyDirectory(string $source, string $dest, int $perm = 0755): bool Recursively copy a directory tree
isDirectoryEmpty() static isDirectoryEmpty(string $dir): bool Return true when the directory has no files
getDirectoryFilesNumber() static getDirectoryFilesNumber(string $dir): int Count files in a directory
findSubDirectories() static findSubDirectories(string $dir, bool $fullPath = false): array List immediate subdirectories
scanDirectory() static scanDirectory(string $dir, ?array $options, ?string $sorting): false|array Scan a directory with optional include-filter and sorting
isThemeDir() static isThemeDir(string $theme, string $default): string Return $theme if it exists, else fall back to $default

Upload — File Upload Handler

Method Signature Description
instance() static instance(string $uploadName, string|int $maxSize, ?string $allowedExt): ?self Get/create the singleton; returns null when no file was submitted or validation fails
check() check(string $uploadName): bool Validate size, extension, and (for images) MIME type integrity
process() process(string $name, string $dir, string $prefix, string|bool $file_name, bool $replace, bool $ext, ?string $convertTo): array Move the uploaded file to $dir; optionally auto-convert the image format. Returns populated fileInfo array on success.

The fileInfo array populated after a successful process:

Key Description
file_name Final filename on disk
ext File extension (possibly changed by convertTo)
name Original browser filename
size File size in bytes
type Full MIME type (from finfo)
type_short MIME prefix only (e.g. image)

Example

PHP
// Handle an avatar upload and auto-convert to WebP
$upload = Upload::instance(
    uploadName:  'avatar',
    maxSize:     '2M',
    allowedExt:  'jpg,jpeg,png,webp'
);

if ($upload) {
    $info = $upload->process(
        name:      'avatar',
        dir:       UPLOADPATH . 'avatars/',
        prefix:    'AVT_',
        convertTo: 'webp'   // auto-convert to WebP after move
    );

    if (array_key_exists('file_name', $info)) {
        Database::Go()->update('users', ['avatar' => $info['file_name']])
            ->where('id', $userId)->run('update');
        Response::success('Avatar updated.', ['file' => $info['file_name']]);
    }
} else {
    // No file submitted — do nothing, or show a message
}