abstract class Files (View source)

File and directory functions

Properties

static protected array $sizeUnits

Supported file size units for the byte conversion functions below

Methods

static string
getUnixStylePath(string $path)

Replacing backslashes and double slashes to slashes.

static string
getNormalizedPath(string $path)

Makes sure path has a trailing slash

static string
concatenatePaths(array $paths)

Properly glues together filepaths / filenames by replacing backslashes and double slashes of the specified paths.

static array
readDirectoryRecursively(string $path, string $suffix = null, bool $returnRealPath = false, bool $returnDotFiles = false)

Returns all filenames from the specified directory. Filters hidden files and directories.

static Generator
getRecursiveDirectoryGenerator(string $path, string $suffix = null, bool $returnRealPath = false, bool $returnDotFiles = false)

No description

static void
emptyDirectoryRecursively(string $path)

Deletes all files, directories and subdirectories from the specified directory. The passed directory itself won't be deleted though.

static void
removeEmptyDirectoriesOnPath(string $path, string $basePath = null)

Removes all empty directories on the specified path. If a base path is given, this function will not remove directories, even if empty, above and including that base path.

static void
removeDirectoryRecursively(string $path)

Deletes all files, directories and subdirectories from the specified directory. Contrary to emptyDirectoryRecursively() this function will also finally remove the emptied directory.

static void
createDirectoryRecursively(string $path)

Creates a directory specified by $path. If the parent directories don't exist yet, they will be created as well.

static void
copyDirectoryRecursively(string $sourceDirectory, string $targetDirectory, bool $keepExistingFiles = false, bool $copyDotFiles = false)

Copies the contents of the source directory to the target directory.

static mixed
getFileContents(string $pathAndFilename, int $flags = 0, resource $context = null, int $offset = 0, int $maximumLength = -1)

An enhanced version of file_get_contents which intercepts the warning issued by the original function if a file could not be loaded.

static string
getUploadErrorMessage(int $errorCode)

Returns a human-readable message for the given PHP file upload error constant.

static bool
is_link(string $pathAndFilename)

A version of is_link() that works on Windows too

static bool
unlink(string $pathAndFilename)

A version of unlink() that works on Windows regardless on the symlink type (file/directory).

static string
bytesToSizeString(float|int $bytes, int $decimals = null, string $decimalSeparator = null, string $thousandsSeparator = null)

Converts an integer with a byte count into human-readable form

static float
sizeStringToBytes(string $sizeString)

Converts a size string (e.g. "1024.0 MB") to the number of bytes it represents

static bool
createRelativeSymlink(string $target, string $link)

Will create relative symlinks by given absolute paths, falling back to Windows' mklink command because PHP's symlink() does not support relative paths there.

static string
getRelativePath(string $from, string $to)

Finds the relative path between two given absolute paths.

Details

static string getUnixStylePath(string $path)

Replacing backslashes and double slashes to slashes.

It's needed to compare paths (especially on windows).

Parameters

string $path

Path which should transformed to the Unix Style.

Return Value

string

static string getNormalizedPath(string $path)

Makes sure path has a trailing slash

Parameters

string $path

Return Value

string

static string concatenatePaths(array $paths)

Properly glues together filepaths / filenames by replacing backslashes and double slashes of the specified paths.

Note: trailing slashes will be removed, leading slashes won't. Usage: concatenatePaths(array('dir1/dir2', 'dir3', 'file'))

Parameters

array $paths

the file paths to be combined. Last array element may include the filename.

Return Value

string

concatenated path without trailing slash.

See also

getUnixStylePath()

static array readDirectoryRecursively(string $path, string $suffix = null, bool $returnRealPath = false, bool $returnDotFiles = false)

Returns all filenames from the specified directory. Filters hidden files and directories.

Parameters

string $path

Path to the directory which shall be read

string $suffix

If specified, only filenames with this extension are returned (eg. ".php" or "foo.bar")

bool $returnRealPath

If turned on, all paths are resolved by calling realpath()

bool $returnDotFiles

If turned on, also files beginning with a dot will be returned

Return Value

array

Filenames including full path

static Generator getRecursiveDirectoryGenerator(string $path, string $suffix = null, bool $returnRealPath = false, bool $returnDotFiles = false)

No description

Parameters

string $path
string $suffix
bool $returnRealPath
bool $returnDotFiles

Return Value

Generator

Exceptions

FilesException

static void emptyDirectoryRecursively(string $path)

Deletes all files, directories and subdirectories from the specified directory. The passed directory itself won't be deleted though.

Parameters

string $path

Path to the directory which shall be emptied.

Return Value

void

Exceptions

FilesException

See also

removeDirectoryRecursively()

static void removeEmptyDirectoriesOnPath(string $path, string $basePath = null)

Removes all empty directories on the specified path. If a base path is given, this function will not remove directories, even if empty, above and including that base path.

Any .DS_Store files are silently removed.

Parameters

string $path

The path on which empty directories shall be removed

string $basePath

A parent path of $path where removal of directories stops

Return Value

void

Exceptions

FilesException

See also

removeDirectoryRecursively()

static void removeDirectoryRecursively(string $path)

Deletes all files, directories and subdirectories from the specified directory. Contrary to emptyDirectoryRecursively() this function will also finally remove the emptied directory.

Parameters

string $path

Path to the directory which shall be removed completely.

Return Value

void

Exceptions

FilesException

See also

emptyDirectoryRecursively()

static void createDirectoryRecursively(string $path)

Creates a directory specified by $path. If the parent directories don't exist yet, they will be created as well.

Make mode configurable / make umask configurable

Parameters

string $path

Path to the directory which shall be created

Return Value

void

Exceptions

FilesException

static void copyDirectoryRecursively(string $sourceDirectory, string $targetDirectory, bool $keepExistingFiles = false, bool $copyDotFiles = false)

Copies the contents of the source directory to the target directory.

$targetDirectory will be created if it does not exist.

If $keepExistingFiles is true, this will keep files already present in the target location. It defaults to false.

If $copyDotFiles is true, this will copy files whose name begin with a dot. It defaults to false.

Parameters

string $sourceDirectory
string $targetDirectory
bool $keepExistingFiles
bool $copyDotFiles

Return Value

void

Exceptions

FilesException

static mixed getFileContents(string $pathAndFilename, int $flags = 0, resource $context = null, int $offset = 0, int $maximumLength = -1)

An enhanced version of file_get_contents which intercepts the warning issued by the original function if a file could not be loaded.

Parameters

string $pathAndFilename

Path and name of the file to load

int $flags

(optional) ORed flags using PHP's FILE_* constants (see manual of file_get_contents).

resource $context

(optional) A context resource created by stream_context_create()

int $offset

(optional) Offset where reading of the file starts, as of PHP 7.1 supports negative offsets.

int $maximumLength

(optional) Maximum length to read. Default is -1 (no limit)

Return Value

mixed

The file content as a string or false if the file could not be opened.

static string getUploadErrorMessage(int $errorCode)

Returns a human-readable message for the given PHP file upload error constant.

Parameters

int $errorCode

One of the UPLOADERR constants

Return Value

string

A version of is_link() that works on Windows too

Parameters

string $pathAndFilename

Path and name of the file or directory

Return Value

bool

true if the path exists and is a symbolic link, false otherwise

See also

http://www.php.net/is_link If http://bugs.php.net/bug.php?id=51766 gets fixed we can drop this.

A version of unlink() that works on Windows regardless on the symlink type (file/directory).

If this method could not unlink the specified file or it doesn't exist anymore (e.g. because of a concurrent deletion), it will clear the stat cache for its filename and check if the file still exist. If it does not exist, this method assumes that the file has been deleted by another process and will return true. If the file still exists though, this method will return false.

Parameters

string $pathAndFilename

Path and name of the file or directory

Return Value

bool

true if file/directory was removed successfully

static string bytesToSizeString(float|int $bytes, int $decimals = null, string $decimalSeparator = null, string $thousandsSeparator = null)

Converts an integer with a byte count into human-readable form

Parameters

float|int $bytes
int $decimals

number of decimal places in the resulting string

string $decimalSeparator

decimal separator of the resulting string

string $thousandsSeparator

thousands separator of the resulting string

Return Value

string

the size string, e.g. "1,024 MB"

static float sizeStringToBytes(string $sizeString)

Converts a size string (e.g. "1024.0 MB") to the number of bytes it represents

Parameters

string $sizeString

the human-readable size string (e.g. ini_get('upload_max_filesize'))

Return Value

float

The number of bytes the $sizeString represents or 0 if the number could not be parsed

Exceptions

FilesException

Will create relative symlinks by given absolute paths, falling back to Windows' mklink command because PHP's symlink() does not support relative paths there.

If the file exists already, it will be deleted regardless of its attributes.

Parameters

string $target

The absolute target where the the symlink should point to relativiely

string $link

The absolute path to the link where the symlink will be created

Return Value

bool

Exceptions

FilesException

static string getRelativePath(string $from, string $to)

Finds the relative path between two given absolute paths.

Credits go to stackoverflow member "Gordon".

Parameters

string $from

An absolute path to base on

string $to

An absolute path to find the relative representation onto $from

Return Value

string

See also

http://stackoverflow.com/questions/2637945/