FileHelper
in package
File Utility.
Table of Contents
Methods
- copyDirectory() : void
- Copy directory.
- copyFile() : void
- Copy file.
- delete() : void
- Delete directory or file.
- find() : array<string|int, mixed>
- Find file.
- findOne() : string|null
- Find only one file.
- findRandomFileName() : string|null
- One is taken at random from the files matching the pattern.
- getDirectorySize() : int
- Get directory size (bytes).
- getMimeByConent() : string
- Get the MIME type predicted from the content of the file.
- getRandomFileContent() : string
- Get the contents of one file taken at random from the files matching the pattern.
- humanFilesize() : string
- Get the file size converted to the appropriate unit.
- makeDirectory() : bool
- Make directory.
- move() : void
- Move a file or directory.
- replace() : void
- Replace file content.
- validationMime() : bool
- Check if the file is of the specified Mime type.
Methods
copyDirectory()
Copy directory.
public
static copyDirectory(string $src, string $dest) : void
Parameters
- $src : string
-
Copy source path.
- $dest : string
-
Copy destination path.
copyFile()
Copy file.
public
static copyFile(string $src, string $dest[, string|null $group = null ][, string|null $user = null ]) : void
Parameters
- $src : string
-
Copy source path.
- $dest : string
-
Copy destination path.
- $group : string|null = null
-
(optional) Owning group.
- $user : string|null = null
-
(optional) Owning user.
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 only one file.
public
static findOne(string $pattern) : string|null
Parameters
- $pattern : string
-
Patterns of files to find.
Return values
string|null —Path of the found file.
findRandomFileName()
One is taken at random from the files matching the pattern.
public
static findRandomFileName(string $pattern) : string|null
Parameters
- $pattern : string
-
Patterns of files to find.
Return values
string|null —Path of the found 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()
Get the MIME type predicted from the content of the file.
public
static getMimeByConent(string $filePath) : string
Parameters
- $filePath : string
-
File Path.
Return values
string —MIME type.
getRandomFileContent()
Get the contents of one file taken at random from the files matching the pattern.
public
static getRandomFileContent(string $pattern) : string
Parameters
- $pattern : string
-
Patterns of files to find.
Return values
string —Contents of the found 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()
Make directory.
public
static makeDirectory(string $dir[, int $mode = 0755 ]) : bool
Parameters
- $dir : string
-
Directory path.
- $mode : int = 0755
-
Permissions. Default is 0755.
Return values
bool —Whether the directory was successfully created or not.
move()
Move a file or directory.
public
static move(string $src, string $dest[, string|null $group = null ][, string|null $user = null ]) : void
Parameters
- $src : string
-
Moving source path.
- $dest : string
-
Destination path.
- $group : string|null = null
-
(optional) Owning group.
- $user : string|null = null
-
(optional) Owning user.
replace()
Replace file content.
public
static replace(string $filePath, array<string|int, mixed> $replacement) : void
Parameters
- $filePath : string
-
File Path.
- $replacement : array<string|int, mixed>
-
Replacement content.
validationMime()
Check if the file is of the specified Mime type.
public
static validationMime(string $filePath, string $mime) : bool
Parameters
- $filePath : string
-
File Path.
- $mime : string
-
MIME type.
Return values
bool —Whether the file is of the specified Mime type.