Constants

MERGE

MERGE = 'merge' : string

Default scheme for Folder::copy Recursively merges subfolders with the same name

OVERWRITE

OVERWRITE = 'overwrite' : string

Overwrite scheme for Folder::copy subfolders with the same name will be replaced

SKIP

SKIP = 'skip' : string

Skip scheme for Folder::copy if a subfolder with the same name exists it will be skipped

SORT_NAME

SORT_NAME = 'name' : string

Sort mode by name

SORT_TIME

SORT_TIME = 'time' : string

Sort mode by time

Properties

$path

$path : string

Path to Folder.

Type

string

$sort

$sort : boolean

Sortedness. Whether or not list results should be sorted by name.

Type

boolean

$mode

$mode : integer

Mode to be used on create. Does nothing on windows platforms.

Type

integer — https://book.cakephp.org/3.0/en/core-libraries/file-folder.html#Cake\Filesystem\Folder::$mode

$_fsorts

$_fsorts : 

Functions array to be called depending on the sort type chosen.

Type

$_messages

$_messages : array

Holds messages from last method.

Type

array

$_errors

$_errors : array

Holds errors from last method.

Type

array

$_directories

$_directories : array

Holds array of complete directory paths.

Type

array

$_files

$_files : array

Holds array of complete file paths.

Type

array

Methods

__construct()

__construct(string|null  $path = null, boolean  $create = false, integer|false  $mode = false) 

Constructor.

Parameters

string|null $path

Path to folder

boolean $create

Create folder if not found

integer|false $mode

Mode (CHMOD) to apply to created folder, false to ignore

pwd()

pwd() : string

Return current path.

Returns

string —

Current path

cd()

cd(string  $path) : string|boolean

Change directory to $path.

Parameters

string $path

Path to the directory to change to

Returns

string|boolean —

The new path. Returns false on failure

read()

read(string|boolean  $sort = self::SORT_NAME, array|boolean  $exceptions = false, boolean  $fullPath = false) : array

Returns an array of the contents of the current directory.

The returned array holds two arrays: One of directories and one of files.

Parameters

string|boolean $sort

Whether you want the results sorted, set this and the sort property to false to get unsorted results.

array|boolean $exceptions

Either an array or boolean true will not grab dot files

boolean $fullPath

True returns the full path

Returns

array —

Contents of current directory as an array, an empty array on failure

find()

find(string  $regexpPattern = '.*', boolean  $sort = false) : array

Returns an array of all matching files in current directory.

Parameters

string $regexpPattern

Preg_match pattern (Defaults to: .*)

boolean $sort

Whether results should be sorted.

Returns

array —

Files that match given pattern

findRecursive()

findRecursive(string  $pattern = '.*', boolean  $sort = false) : array

Returns an array of all matching files in and below current directory.

Parameters

string $pattern

Preg_match pattern (Defaults to: .*)

boolean $sort

Whether results should be sorted.

Returns

array —

Files matching $pattern

isWindowsPath()

isWindowsPath(string  $path) : boolean

Returns true if given $path is a Windows path.

Parameters

string $path

Path to check

Returns

boolean —

true if windows path, false otherwise

isAbsolute()

isAbsolute(string  $path) : boolean

Returns true if given $path is an absolute path.

Parameters

string $path

Path to check

Returns

boolean —

true if path is absolute.

isRegisteredStreamWrapper()

isRegisteredStreamWrapper(string  $path) : boolean

Returns true if given $path is a registered stream wrapper.

Parameters

string $path

Path to check

Returns

boolean —

True if path is registered stream wrapper.

normalizePath()

normalizePath(string  $path) : string

Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)

Parameters

string $path

Path to check

Returns

string —

Set of slashes ("\" or "/")

normalizeFullPath()

normalizeFullPath(string  $path) : string

Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)

Parameters

string $path

Path to transform

Returns

string —

Path with the correct set of slashes ("\" or "/")

correctSlashFor()

correctSlashFor(string  $path) : string

Returns a correct set of slashes for given $path. (\\ for Windows paths and / for other paths.)

Parameters

string $path

Path to check

Returns

string —

Set of slashes ("\" or "/")

slashTerm()

slashTerm(string  $path) : string

Returns $path with added terminating slash (corrected for Windows or other OS).

Parameters

string $path

Path to check

Returns

string —

Path with ending slash

addPathElement()

addPathElement(string  $path, string|array  $element) : string

Returns $path with $element added, with correct slash in-between.

Parameters

string $path

Path

string|array $element

Element to add at end of path

Returns

string —

Combined path

inCakePath()

inCakePath(string  $path = '') : boolean

Returns true if the Folder is in the given Cake path.

Parameters

string $path

The path to check.

Returns

boolean

inPath()

inPath(string  $path, boolean  $reverse = false) : boolean

Returns true if the Folder is in the given path.

Parameters

string $path

The absolute path to check that the current pwd() resides within.

boolean $reverse

Reverse the search, check if the given $path resides within the current pwd().

Throws

\InvalidArgumentException

When the given $path argument is not an absolute path.

Returns

boolean

chmod()

chmod(string  $path, integer|boolean  $mode = false, boolean  $recursive = true, array  $exceptions = array()) : boolean

Change the mode on a directory structure recursively. This includes changing the mode on files as well.

Parameters

string $path

The path to chmod.

integer|boolean $mode

Octal value, e.g. 0755.

boolean $recursive

Chmod recursively, set to false to only change the current directory.

array $exceptions

Array of files, directories to skip.

Returns

boolean —

Success.

subdirectories()

subdirectories(string|null  $path = null, boolean  $fullPath = true) : array

Returns an array of subdirectories for the provided or current path.

Parameters

string|null $path

The directory path to get subdirectories for.

boolean $fullPath

Whether to return the full path or only the directory name.

Returns

array —

Array of subdirectories for the provided or current path.

tree()

tree(string|null  $path = null, array|boolean  $exceptions = false, string|null  $type = null) : array

Returns an array of nested directories and files in each directory

Parameters

string|null $path

the directory path to build the tree from

array|boolean $exceptions

Either an array of files/folder to exclude or boolean true to not grab dot files/folders

string|null $type

either 'file' or 'dir'. Null returns both files and directories

Returns

array —

Array of nested directories and files in each directory

create()

create(string  $pathname, integer|boolean  $mode = false) : boolean

Create a directory structure recursively.

Can be used to create deep path structures like /foo/bar/baz/shoe/horn

Parameters

string $pathname

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().

integer|boolean $mode

octal value 0755

Returns

boolean —

Returns TRUE on success, FALSE on failure

dirsize()

dirsize() : integer

Returns the size in bytes of this Folder and its contents.

Returns

integer —

size in bytes of current folder

delete()

delete(string|null  $path = null) : boolean

Recursively Remove directories if the system allows.

Parameters

string|null $path

Path of directory to delete

Returns

boolean —

Success

copy()

copy(array|string  $options) : boolean

Recursive directory copy.

Options

  • to The directory to copy to.
  • from The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
  • mode The mode to copy the files/directories with as integer, e.g. 0775.
  • skip Files/directories to skip.
  • scheme Folder::MERGE, Folder::OVERWRITE, Folder::SKIP
  • recursive Whether to copy recursively or not (default: true - recursive)

Parameters

array|string $options

Either an array of options (see above) or a string of the destination directory.

Returns

boolean —

Success.

move()

move(array|string  $options) : boolean

Recursive directory move.

Options

  • to The directory to copy to.
  • from The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
  • chmod The mode to copy the files/directories with.
  • skip Files/directories to skip.
  • scheme Folder::MERGE, Folder::OVERWRITE, Folder::SKIP
  • recursive Whether to copy recursively or not (default: true - recursive)

Parameters

array|string $options

(to, from, chmod, skip, scheme)

Returns

boolean —

Success

messages()

messages(boolean  $reset = true) : array

get messages from latest method

Parameters

boolean $reset

Reset message stack after reading

Returns

array

errors()

errors(boolean  $reset = true) : array

get error from latest method

Parameters

boolean $reset

Reset error stack after reading

Returns

array

realpath()

realpath(string  $path) : string|false

Get the real path (taking ".

." and such into account)

Parameters

string $path

Path to resolve

Returns

string|false —

The resolved path

isSlashTerm()

isSlashTerm(string  $path) : boolean

Returns true if given $path ends in a slash (i.e. is slash-terminated).

Parameters

string $path

Path to check

Returns

boolean —

true if path ends with slash, false otherwise

_findRecursive()

_findRecursive(string  $pattern, boolean  $sort = false) : array

Private helper function for findRecursive.

Parameters

string $pattern

Pattern to match against

boolean $sort

Whether results should be sorted.

Returns

array —

Files matching pattern