Folder
in package
Folder structure browser, lists folders and files.
Will be removed in 5.0.
Provides an Object interface for Common directory related tasks.
Tags
Table of Contents
Constants
- MERGE = 'merge'
- Default scheme for Folder::copy Recursively merges subfolders with the same name
- OVERWRITE = 'overwrite'
- Overwrite scheme for Folder::copy subfolders with the same name will be replaced
- SKIP = 'skip'
- Skip scheme for Folder::copy if a subfolder with the same name exists it will be skipped
- SORT_NAME = 'name'
- Sort mode by name
- SORT_TIME = 'time'
- Sort mode by time
Properties
- $mode : int
- Mode to be used on create. Does nothing on windows platforms.
- $path : string
- Path to Folder.
- $sort : bool
- Sortedness. Whether list results should be sorted by name.
- $_directories : array<string|int, mixed>
- Holds array of complete directory paths.
- $_errors : array<string|int, mixed>
- Holds errors from last method.
- $_files : array<string|int, mixed>
- Holds array of complete file paths.
- $_fsorts : array<string|int, string>
- Functions array to be called depending on the sort type chosen.
- $_messages : array<string|int, mixed>
- Holds messages from last method.
Methods
- __construct() : mixed
- Constructor.
- addPathElement() : string
- Returns $path with $element added, with correct slash in-between.
- cd() : string|false
- Change directory to $path.
- chmod() : bool
- Change the mode on a directory structure recursively. This includes changing the mode on files as well.
- copy() : bool
- Recursive directory copy.
- correctSlashFor() : string
- Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
- create() : bool
- Create a directory structure recursively.
- delete() : bool
- Recursively Remove directories if the system allows.
- dirsize() : int
- Returns the size in bytes of this Folder and its contents.
- errors() : array<string|int, mixed>
- get error from latest method
- find() : array<string|int, string>
- Returns an array of all matching files in current directory.
- findRecursive() : array<string|int, mixed>
- Returns an array of all matching files in and below current directory.
- inPath() : bool
- Returns true if the Folder is in the given path.
- isAbsolute() : bool
- Returns true if given $path is an absolute path.
- isRegisteredStreamWrapper() : bool
- Returns true if given $path is a registered stream wrapper.
- isSlashTerm() : bool
- Returns true if given $path ends in a slash (i.e. is slash-terminated).
- isWindowsPath() : bool
- Returns true if given $path is a Windows path.
- messages() : array<string|int, mixed>
- get messages from latest method
- move() : bool
- Recursive directory move.
- normalizeFullPath() : string
- Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
- pwd() : string|null
- Return current path.
- read() : array<string|int, mixed>
- Returns an array of the contents of the current directory.
- realpath() : string|false
- Get the real path (taking ".." and such into account)
- slashTerm() : string
- Returns $path with added terminating slash (corrected for Windows or other OS).
- subdirectories() : array<string|int, mixed>
- Returns an array of subdirectories for the provided or current path.
- tree() : array<string|int, mixed>
- Returns an array of nested directories and files in each directory
- _findRecursive() : array<string|int, mixed>
- Private helper function for findRecursive.
Constants
MERGE
Default scheme for Folder::copy Recursively merges subfolders with the same name
public
string
MERGE
= 'merge'
OVERWRITE
Overwrite scheme for Folder::copy subfolders with the same name will be replaced
public
string
OVERWRITE
= 'overwrite'
SKIP
Skip scheme for Folder::copy if a subfolder with the same name exists it will be skipped
public
string
SKIP
= 'skip'
SORT_NAME
Sort mode by name
public
string
SORT_NAME
= 'name'
SORT_TIME
Sort mode by time
public
string
SORT_TIME
= 'time'
Properties
$mode
Mode to be used on create. Does nothing on windows platforms.
public
int
$mode
= 0755
https://book.cakephp.org/4/en/core-libraries/file-folder.html#Cake\Filesystem\Folder::$mode
$path
Path to Folder.
public
string
$path
$sort
Sortedness. Whether list results should be sorted by name.
public
bool
$sort
= false
$_directories
Holds array of complete directory paths.
protected
array<string|int, mixed>
$_directories
$_errors
Holds errors from last method.
protected
array<string|int, mixed>
$_errors
= []
$_files
Holds array of complete file paths.
protected
array<string|int, mixed>
$_files
$_fsorts
Functions array to be called depending on the sort type chosen.
protected
array<string|int, string>
$_fsorts
= [self::SORT_NAME => 'getPathname', self::SORT_TIME => 'getCTime']
$_messages
Holds messages from last method.
protected
array<string|int, mixed>
$_messages
= []
Methods
__construct()
Constructor.
public
__construct([string|null $path = null ][, bool $create = false ][, int|null $mode = null ]) : mixed
Parameters
- $path : string|null = null
-
Path to folder
- $create : bool = false
-
Create folder if not found
- $mode : int|null = null
-
Mode (CHMOD) to apply to created folder, false to ignore
addPathElement()
Returns $path with $element added, with correct slash in-between.
public
static addPathElement(string $path, array<string|int, string>|string $element) : string
Parameters
- $path : string
-
Path
- $element : array<string|int, string>|string
-
Element to add at end of path
Return values
string —Combined path
cd()
Change directory to $path.
public
cd(string $path) : string|false
Parameters
- $path : string
-
Path to the directory to change to
Return values
string|false —The new path. Returns false on failure
chmod()
Change the mode on a directory structure recursively. This includes changing the mode on files as well.
public
chmod(string $path[, int|null $mode = null ][, bool $recursive = true ][, array<string|int, string> $exceptions = [] ]) : bool
Parameters
- $path : string
-
The path to chmod.
- $mode : int|null = null
-
Octal value, e.g. 0755.
- $recursive : bool = true
-
Chmod recursively, set to false to only change the current directory.
- $exceptions : array<string|int, string> = []
-
Array of files, directories to skip.
Return values
bool —Success.
copy()
Recursive directory copy.
public
copy(string $to[, array<string, mixed> $options = [] ]) : bool
Options
-
from
The directory to copy from, this will cause a cd() to occur, changing the results ofpwd()
. -
mode
The mode to copy the files/directories with as integer, e.g.0770
. -
skip
Files/directories to skip. -
scheme
Folder::MERGE, Folder::OVERWRITE, Folder::SKIP -
recursive
Whether to copy recursively or not (default: true - recursive)
Parameters
- $to : string
-
The directory to copy to.
- $options : array<string, mixed> = []
-
Array of options (see above).
Return values
bool —Success.
correctSlashFor()
Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
public
static correctSlashFor(string $path) : string
Parameters
- $path : string
-
Path to check
Return values
string —Set of slashes ("\" or "/")
create()
Create a directory structure recursively.
public
create(string $pathname[, int|null $mode = null ]) : bool
Can be used to create deep path structures like /foo/bar/baz/shoe/horn
Parameters
- $pathname : string
-
The directory structure to create. Either an absolute or relative path. If the path is relative and exists in the process' cwd it will not be created. Otherwise, relative paths will be prefixed with the current pwd().
- $mode : int|null = null
-
octal value 0755
Return values
bool —Returns TRUE on success, FALSE on failure
delete()
Recursively Remove directories if the system allows.
public
delete([string|null $path = null ]) : bool
Parameters
- $path : string|null = null
-
Path of directory to delete
Return values
bool —Success
dirsize()
Returns the size in bytes of this Folder and its contents.
public
dirsize() : int
Return values
int —size in bytes of current folder
errors()
get error from latest method
public
errors([bool $reset = true ]) : array<string|int, mixed>
Parameters
- $reset : bool = true
-
Reset error stack after reading
Return values
array<string|int, mixed>find()
Returns an array of all matching files in current directory.
public
find([string $regexpPattern = '.*' ][, string|bool $sort = false ]) : array<string|int, string>
Parameters
- $regexpPattern : string = '.*'
-
Preg_match pattern (Defaults to: .*)
- $sort : string|bool = false
-
Whether results should be sorted.
Return values
array<string|int, string> —Files that match given pattern
findRecursive()
Returns an array of all matching files in and below current directory.
public
findRecursive([string $pattern = '.*' ][, string|bool $sort = false ]) : array<string|int, mixed>
Parameters
- $pattern : string = '.*'
-
Preg_match pattern (Defaults to: .*)
- $sort : string|bool = false
-
Whether results should be sorted.
Return values
array<string|int, mixed> —Files matching $pattern
inPath()
Returns true if the Folder is in the given path.
public
inPath(string $path[, bool $reverse = false ]) : bool
Parameters
- $path : string
-
The absolute path to check that the current
pwd()
resides within. - $reverse : bool = false
-
Reverse the search, check if the given
$path
resides within the currentpwd()
.
Tags
Return values
boolisAbsolute()
Returns true if given $path is an absolute path.
public
static isAbsolute(string $path) : bool
Parameters
- $path : string
-
Path to check
Return values
bool —true if path is absolute.
isRegisteredStreamWrapper()
Returns true if given $path is a registered stream wrapper.
public
static isRegisteredStreamWrapper(string $path) : bool
Parameters
- $path : string
-
Path to check
Return values
bool —True if path is registered stream wrapper.
isSlashTerm()
Returns true if given $path ends in a slash (i.e. is slash-terminated).
public
static isSlashTerm(string $path) : bool
Parameters
- $path : string
-
Path to check
Return values
bool —true if path ends with slash, false otherwise
isWindowsPath()
Returns true if given $path is a Windows path.
public
static isWindowsPath(string $path) : bool
Parameters
- $path : string
-
Path to check
Return values
bool —true if windows path, false otherwise
messages()
get messages from latest method
public
messages([bool $reset = true ]) : array<string|int, mixed>
Parameters
- $reset : bool = true
-
Reset message stack after reading
Return values
array<string|int, mixed>move()
Recursive directory move.
public
move(string $to[, array<string, mixed> $options = [] ]) : bool
Options
-
from
The directory to copy from, this will cause a cd() to occur, changing the results ofpwd()
. -
mode
The mode to copy the files/directories with as integer, e.g.0770
. -
skip
Files/directories to skip. -
scheme
Folder::MERGE, Folder::OVERWRITE, Folder::SKIP -
recursive
Whether to copy recursively or not (default: true - recursive)
Parameters
- $to : string
-
The directory to move to.
- $options : array<string, mixed> = []
-
Array of options (see above).
Return values
bool —Success
normalizeFullPath()
Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)
public
static normalizeFullPath(string $path) : string
Parameters
- $path : string
-
Path to transform
Return values
string —Path with the correct set of slashes ("\" or "/")
pwd()
Return current path.
public
pwd() : string|null
Return values
string|null —Current path
read()
Returns an array of the contents of the current directory.
public
read([string|bool $sort = self::SORT_NAME ][, array<string|int, mixed>|bool $exceptions = false ][, bool $fullPath = false ]) : array<string|int, mixed>
The returned array holds two arrays: One of directories and one of files.
Parameters
- $sort : string|bool = self::SORT_NAME
-
Whether you want the results sorted, set this and the sort property to false to get unsorted results.
- $exceptions : array<string|int, mixed>|bool = false
-
Either an array or boolean true will not grab dot files
- $fullPath : bool = false
-
True returns the full path
Return values
array<string|int, mixed> —Contents of current directory as an array, an empty array on failure
realpath()
Get the real path (taking ".." and such into account)
public
realpath(string $path) : string|false
Parameters
- $path : string
-
Path to resolve
Return values
string|false —The resolved path
slashTerm()
Returns $path with added terminating slash (corrected for Windows or other OS).
public
static slashTerm(string $path) : string
Parameters
- $path : string
-
Path to check
Return values
string —Path with ending slash
subdirectories()
Returns an array of subdirectories for the provided or current path.
public
subdirectories([string|null $path = null ][, bool $fullPath = true ]) : array<string|int, mixed>
Parameters
- $path : string|null = null
-
The directory path to get subdirectories for.
- $fullPath : bool = true
-
Whether to return the full path or only the directory name.
Return values
array<string|int, mixed> —Array of subdirectories for the provided or current path.
tree()
Returns an array of nested directories and files in each directory
public
tree([string|null $path = null ][, array<string|int, mixed>|bool $exceptions = false ][, string|null $type = null ]) : array<string|int, mixed>
Parameters
- $path : string|null = null
-
the directory path to build the tree from
- $exceptions : array<string|int, mixed>|bool = false
-
Either an array of files/folder to exclude or boolean true to not grab dot files/folders
- $type : string|null = null
-
either 'file' or 'dir'. Null returns both files and directories
Return values
array<string|int, mixed> —Array of nested directories and files in each directory
_findRecursive()
Private helper function for findRecursive.
protected
_findRecursive(string $pattern[, bool $sort = false ]) : array<string|int, mixed>
Parameters
- $pattern : string
-
Pattern to match against
- $sort : bool = false
-
Whether results should be sorted.
Return values
array<string|int, mixed> —Files matching pattern