class FileSystem extends FileSystem (View source)

The FileSystem class provides all methods related to file system operations.

Properties

static private $packageDirectoryItems The cached array of items in the packages directory.

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.

static string
appendSuffixToPath(string $path, string $suffix)

Append a suffix to a path just before the trailing slash.

static 
appendSuffixToTitle(string $path, string $suffix)

Open a data text file under the given path, read the data, append a suffix to the title variable and write back the data.

static 
copyPageFiles(string $source, string $dest)

Unlike self::movePageDir(), this method only copies all files within a page directory without (!) any subdirectories.

static bool
deleteMedia(string $file, Messenger $Messenger)

Deletes a file and its caption (if existing).

static string
fullPagePath(string $path)

Get the full file system path for the given path.

static array
getPackagesDirectoryItems(string $filter = '')

Get all items in the packages directory, optionally filtered by a regex string.

static string
getPathByPostUrl(Automad $Automad)

Return the file system path for the directory of a page based on $_POST['url'].

static array
listDirectoryRecursively(string $directory, string $base = AM_BASE_DIR)

Recursively list all items in a directory.

static string
movePageDir(string $oldPath, string $newParentPath, string $prefix, string $slug)

Move a directory to a new location.

static string
purgeCache()

Move all items in /cache to the PHP temp directory.

static bool
renameMedia(string $oldFile, string $newFile, Messenger $Messenger)

Renames a file and its caption (if existing).

static string
uniquePathSuffix(string $path, string $prefix = '')

Creates an unique suffix for a path to avoid conflicts with existing directories.

static 
writeData(array $data, string $file)

Format, filter and write the data array a text file.

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

static string appendSuffixToPath(string $path, string $suffix)

Append a suffix to a path just before the trailing slash.

Parameters

string $path
string $suffix

Return Value

string The path with appended suffix

static appendSuffixToTitle(string $path, string $suffix)

Open a data text file under the given path, read the data, append a suffix to the title variable and write back the data.

Parameters

string $path
string $suffix

static copyPageFiles(string $source, string $dest)

Unlike self::movePageDir(), this method only copies all files within a page directory without (!) any subdirectories.

Parameters

string $source
string $dest

static bool deleteMedia(string $file, Messenger $Messenger)

Deletes a file and its caption (if existing).

Parameters

string $file
Messenger $Messenger

Return Value

bool true on success

static string fullPagePath(string $path)

Get the full file system path for the given path.

Parameters

string $path

Return Value

string The full path

static array getPackagesDirectoryItems(string $filter = '')

Get all items in the packages directory, optionally filtered by a regex string.

Parameters

string $filter

Return Value

array A filtered list with all items in the packages directory

static string getPathByPostUrl(Automad $Automad)

Return the file system path for the directory of a page based on $_POST['url'].

In case URL is empty, return the '/shared' directory.

Parameters

Automad $Automad

Return Value

string The full path to the related directory

static array listDirectoryRecursively(string $directory, string $base = AM_BASE_DIR)

Recursively list all items in a directory.

Parameters

string $directory
string $base

Return Value

array The list of items

static string movePageDir(string $oldPath, string $newParentPath, string $prefix, string $slug)

Move a directory to a new location.

The final path is composed of the parent directoy, the prefix and the title. In case the resulting path is already occupied, an index get appended to the prefix, to be reproducible when resaving the page.

Parameters

string $oldPath
string $newParentPath (destination)
string $prefix
string $slug

Return Value

string $newPath

static string purgeCache()

Move all items in /cache to the PHP temp directory.

Return Value

string $tmp

static bool renameMedia(string $oldFile, string $newFile, Messenger $Messenger)

Renames a file and its caption (if existing).

Parameters

string $oldFile
string $newFile
Messenger $Messenger

Return Value

bool true on success

static string uniquePathSuffix(string $path, string $prefix = '')

Creates an unique suffix for a path to avoid conflicts with existing directories.

Parameters

string $path
string $prefix (prepended to the numerical suffix)

Return Value

string The suffix

static writeData(array $data, string $file)

Format, filter and write the data array a text file.

Parameters

array $data
string $file