Documentation

FileHelper
in package

FinalYes

File system utility class.

Provides file and directory operations including creation, deletion, copying, permission handling, and path manipulation.

Table of Contents

Methods

copyDirectory()  : void
Copy a directory recursively.
copyFile()  : void
Copy a single file.
delete()  : void
Delete directory or file.
find()  : array<string|int, mixed>
Find file.
findOne()  : string|null
Find a single file matching a glob pattern.
findRandomFileName()  : string
Get a random filename from files matching a glob pattern.
getDirectorySize()  : int
Get directory size (bytes).
getMimeByConent()  : string
Detect MIME type from file content using finfo.
getRandomFileContent()  : string
Read the contents of a random file matching a glob pattern.
humanFilesize()  : string
Get the file size converted to the appropriate unit.
makeDirectory()  : bool
Create a directory recursively.
move()  : void
Move (rename) a file or directory.
replace()  : void
Replace strings in a file using search/replace pairs.
validationMime()  : bool
Validate that a file matches the expected MIME type.

Methods

copyDirectory()

Copy a directory recursively.

public static copyDirectory(string $src, string $dest) : void
Parameters
$src : string

Source directory path.

$dest : string

Destination directory path.

Tags
throws
RuntimeException

If source does not exist or is not a directory.

copyFile()

Copy a single file.

public static copyFile(string $src, string $dest[, string|null $group = null ][, string|null $user = null ]) : void

Creates destination directory if it does not exist.

Parameters
$src : string

Source file path.

$dest : string

Destination file path.

$group : string|null = null

Ownership group to set on destination.

$user : string|null = null

Ownership user to set on destination.

Tags
throws
RuntimeException

If source does not exist, is not a file, or copy fails.

delete()

Delete directory or file.

public static delete(string ...$paths) : void
use \X\Util\FileHelper;

// Delete all files and folders in "/ path"..
FileHelper::delete('/test');

// Delete all files and folders in the "/ path" folder and also in the "/ path" folder.
$deleteSelf = true;
FileHelper::delete('/test', $deleteSelf);

// Lock before deleting, Locks are disabled by default.
$deleteSelf = true;
$enableLock = true;
FileHelper::delete('/test', $deleteSelf, $enableLock);
Parameters
$paths : string

Path to be deleted.

find()

Find file.

public static find(string $pattern[, int $flags = 0 ]) : array<string|int, mixed>
use \X\Util\FileHelper;

// Search only image files.
FileHelper::find('/img/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
Parameters
$pattern : string

Patterns of files to find.

$flags : int = 0

GLOB_MARK: Adds a slash (a backslash on Windows) to each directory returned GLOB_NOSORT: Return files as they appear in the directory (no sorting). When this flag is not used, the pathnames are sorted alphabetically GLOB_NOCHECK: Return the search pattern if no files matching it were found GLOB_NOESCAPE: Backslashes do not quote metacharacters GLOB_BRACE: Expands {a,b,c} to match 'a', 'b', or 'c' GLOB_ONLYDIR: Return only directory entries which match the pattern GLOB_ERR: Stop on read errors (like unreadable directories), by default errors are ignored.

Return values
array<string|int, mixed>

List of files found.

findOne()

Find a single file matching a glob pattern.

public static findOne(string $pattern) : string|null
Parameters
$pattern : string

Glob pattern.

Return values
string|null

Basename of the first match, or null if none found.

findRandomFileName()

Get a random filename from files matching a glob pattern.

public static findRandomFileName(string $pattern) : string
Parameters
$pattern : string

Glob pattern.

Return values
string

Basename of a randomly selected file.

getDirectorySize()

Get directory size (bytes).

public static getDirectorySize(string|array<string|int, string> $dirs[, array<string|int, mixed> &$infos = [] ]) : int
use \X\Util\FileHelper;

// Returns the total size of all files in a directory
FileHelper::getDirectorySize('/var/log');

// Returns the total size of all files in multiple directories
FileHelper::getDirectorySize([ '/var/log/php-fpm' '/var/log/nginx' ]);
Parameters
$dirs : string|array<string|int, string>

Directory path. Multiple can also be specified in an array.

$infos : array<string|int, mixed> = []

(optional) Information on directories found.

Return values
int

Directory size in bytes.

getMimeByConent()

Detect MIME type from file content using finfo.

public static getMimeByConent(string $filePath) : string
Parameters
$filePath : string

File path.

Tags
throws
RuntimeException

If file does not exist or is not a file.

Return values
string

MIME type string (e.g., "image/png").

getRandomFileContent()

Read the contents of a random file matching a glob pattern.

public static getRandomFileContent(string $pattern) : string
Parameters
$pattern : string

Glob pattern.

Return values
string

File contents of a randomly selected file.

humanFilesize()

Get the file size converted to the appropriate unit.

public static humanFilesize(string $filePath[, int $decimals = 2 ]) : string
use \X\Util\FileHelper;

FileHelper::humanFilesize('/var/somefile.txt', 0);// 12B
FileHelper::humanFilesize('/var/somefile.txt', 4);// 1.1498GB
FileHelper::humanFilesize('/var/somefile.txt', 1);// 117.7MB
FileHelper::humanFilesize('/var/somefile.txt', 5);// 11.22833TB
FileHelper::humanFilesize('/var/somefile.txt', 3);// 1.177MB
FileHelper::humanFilesize('/var/somefile.txt');// 120.56KB
Parameters
$filePath : string

File Path.

$decimals : int = 2

(optional) Decimal digits. Default is 2.

Return values
string

File size with units.

makeDirectory()

Create a directory recursively.

public static makeDirectory(string $dir[, int $mode = 0755 ]) : bool

Does nothing if the directory already exists.

Parameters
$dir : string

Directory path to create.

$mode : int = 0755

Directory permissions. Default is 0755.

Return values
bool

True if created, false if already exists or creation failed.

move()

Move (rename) a file or directory.

public static move(string $src, string $dest[, string|null $group = null ][, string|null $user = null ]) : void

If the destination contains no directory separator, the file is moved within the same directory. Missing extension is inherited from source.

Parameters
$src : string

Source path.

$dest : string

Destination path or filename.

$group : string|null = null

Ownership group to set on destination.

$user : string|null = null

Ownership user to set on destination.

Tags
throws
RuntimeException

If source does not exist or rename fails.

replace()

Replace strings in a file using search/replace pairs.

public static replace(string $filePath, array<string|int, mixed> $replacement) : void
Parameters
$filePath : string

File path to modify.

$replacement : array<string|int, mixed>

Associative array of search => replace pairs.

validationMime()

Validate that a file matches the expected MIME type.

public static validationMime(string $filePath, string $mime) : bool
Parameters
$filePath : string

File path.

$mime : string

Expected MIME type (e.g., "image/jpeg").

Return values
bool

True if the file's detected MIME type matches.


        
On this page

Search results