The fDirectory class is a simple object representation of a directory on the filesystem. It provides an object-based interface to common directory functions and allows actions to be grouped into transactions via the fFilesystem class.
The fDirectory constructor takes a single argument, the filesystem path to a directory. The path can be absolute or relative.
$directory1 = new fDirectory('/var/www/vhosts/examples.com/httpdocs/images/'); $directory2 = new fDirectory('./output/'); $directory3 = new fDirectory('../uploads/documents/');
It is also possible to create a directory on the filesystem and instantiate an fDirectory object for that new directory by calling the static method
create() takes up to two parameters, with the first being
$directory_path and the second optional parameter being
$mode parameter allows setting the permissions for the new directory, and usually is set using an octal number (represented by a number with a leading zero such as 0777).
Please note that directory creation is recursive, so any non-existant parent directories will also be created as needed.
$new_directory = fDirectory::create('./logs/');
The fDirectory class includes a few methods to grab some basic information about a directory. These include:
||Returns the name of the directory as a string - does not include the full path|
||Returns the size of the directory and all contents in bytes as an integer, or optionally formatted for easy human readability|
||Returns the fDirectory object for the directorys parent directory|
||Returns the full path to the directory as a string|
||Indicates if the directory can be written to by the current user|
If you want more specific information about a directory, you can pass the output of
getPath() into the various PHP filesystem functions.
The following methods provide a straight-forward interface for some standard manipulation of directories. Also note that these manipulations can be wrapped in a filesystem transactions to allow for rolling back changes in the event of a later error.
||Renames the directory to a new path|
||Moves the directory into a different parent directory, keeping the current name|
||Removes the directory and all contained files and folders from the filesystem. Please note that if inside of a filesystem transaction, this event will be deferred until commit is called, but instances of fDirectory and fFile for all affected files and directories will act as if the directory/file no longer exists.|
||Removes all contained files and folders from the filesystem. Please note that if inside of a filesystem transaction, this event will be deferred until commit is called, but instances of fDirectory and fFile for all affected files and directories will act as if the directory/file no longer exists.|
scanRecursive() method provide functionality for listing all fFile, fImage and fDirectory children of an fDirectory object. Both return an array of objects, however
scan() returns only direct children, whereas
scanRecursive() returns all descendants.
$children = $dir->scan(); $descendants = $dir->scanRecursive();
Both methods accept a single optional parameter,
$filter. The filter can be a valid PCRE regex pattern, or a string pattern containing
* will match zero or more characters, while
? will match zero or one characters. When matching the
$filter, all file paths will have
/ directory separators, and all directories will end in
// This will list all directory children $directories = $dir->scan('*/'); // This will find all jpg and gif files under the current directory $images = $dir->scanRecursive('#\.(jpe?g|gif)$#i');