class FileSystem (View source)

The FileSystem class.

Methods

static bool
deleteFile(string $file)

Delete a file.

static string
getExtension(string $file)

Return the extension for a given file.

static mixed
getImageExtensionFromMimeType(string $file)

Get file extension for images based on mime types.

static string
getTmpDir()

Return the path of the temp dir if it is writable by the webserver.

static array
glob(string $pattern, int $flags = 0)

A wrapper for PHP's built-in glob function.

static array
globGrep(string $pattern, string $regex, int $flags = 0)

Find files by using the glob() method and filter the resulting array by a regex pattern.

static bool
isAllowedFileType(string $str)

Tests if a string is a file name with an allowed file extension.

static bool
makeDir(string $path)

Create directory if not existing.

static string
normalizeSlashes(string $path)

Replace all backslashes in a given path with forward slashes.

static bool
write(string $file, string $content)

Write content to a file and create the parent directory if needed.

Details

static bool deleteFile(string $file)

Delete a file.

Parameters

string $file

Return Value

bool Return true if the file was deleted succsessfully

static string getExtension(string $file)

Return the extension for a given file.

Parameters

string $file

Return Value

string The extension

static mixed getImageExtensionFromMimeType(string $file)

Get file extension for images based on mime types.

Parameters

string $file

Return Value

mixed The extension or false

static string getTmpDir()

Return the path of the temp dir if it is writable by the webserver.

In any case, '/tmp' is the preferred directory, because of automatic cleanup at reboot, while other locations like '/var/tmp' do not get purged by the system. But since '/tmp' is only available on macos and linux, sys_get_temp_dir() is used as fallback.

Return Value

string The path to the temp dir

static array glob(string $pattern, int $flags = 0)

A wrapper for PHP's built-in glob function.

This method always returns an array, even though glob() returns false on some systems instead of empty arrays.

Parameters

string $pattern
int $flags

Return Value

array The list of matching files

static array globGrep(string $pattern, string $regex, int $flags = 0)

Find files by using the glob() method and filter the resulting array by a regex pattern.

Note that this method should basically replace the usage of GLOB_BRACE to be fully compatible to systems where this constant is not defined. Instead of a glob pattern like "/path/.{jpg,png}" it is more safe to use a generic pattern like "/path/.*" filtered by a regex like "/.(jpg|png)$/i" without using the GLOB_BRACE flag.

Parameters

string $pattern
string $regex
int $flags

Return Value

array The filtered list of matching files

static bool isAllowedFileType(string $str)

Tests if a string is a file name with an allowed file extension.

Basically a possibly existing file extension is checked against the array of allowed file extensions.

"/url/file.jpg" will return true, "/url/file" or "/url/file.something" will return false.

Parameters

string $str

Return Value

bool

static bool makeDir(string $path)

Create directory if not existing.

Parameters

string $path

Return Value

bool True on success, else false

static string normalizeSlashes(string $path)

Replace all backslashes in a given path with forward slashes.

Parameters

string $path

Return Value

string The processed path with only forward slashes

static bool write(string $file, string $content)

Write content to a file and create the parent directory if needed.

Parameters

string $file
string $content

Return Value

bool True on success, else false