class Filesystem

Provides basic utility to manipulate the file system.

Methods

copy(string $originFile, string $targetFile, bool $overwriteNewerFiles = false)

Copies a file.

mkdir(string|iterable $dirs, int $mode = 0777)

Creates a directory recursively.

bool
exists(string|iterable $files)

Checks the existence of files or directories.

touch(string|iterable $files, int $time = null, int $atime = null)

Sets access and modification time of file.

remove(string|iterable $files)

Removes files or directories.

chmod(string|iterable $files, int $mode, int $umask = 00, bool $recursive = false)

Change mode for an array of files or directories.

chown(string|iterable $files, string $user, bool $recursive = false)

Change the owner of an array of files or directories.

chgrp(string|iterable $files, string $group, bool $recursive = false)

Change the group of an array of files or directories.

rename(string $origin, string $target, bool $overwrite = false)

Renames a file or a directory.

symlink(string $originDir, string $targetDir, bool $copyOnWindows = false)

Creates a symbolic link or copy a directory.

hardlink(string $originFile, string|string[] $targetFiles)

Creates a hard link, or several hard links to a file.

string|null
readlink(string $path, bool $canonicalize = false)

Resolves links in paths.

string
makePathRelative(string $endPath, string $startPath)

Given an existing path, convert it to a path relative to a given starting path.

mirror(string $originDir, string $targetDir, Traversable $iterator = null, array $options = array())

Mirrors a directory to another.

bool
isAbsolutePath(string $file)

Returns whether the file path is an absolute path.

string
tempnam(string $dir, string $prefix)

Creates a temporary file with support for custom stream wrappers.

dumpFile(string $filename, string $content)

Atomically dumps content into a file.

appendToFile(string $filename, string $content)

Appends content to an existing file.

static 
handleError($type, $msg)

No description

Details

copy(string $originFile, string $targetFile, bool $overwriteNewerFiles = false)

Copies a file.

If the target file is older than the origin file, it's always overwritten. If the target file is newer, it is overwritten only when the $overwriteNewerFiles option is set to true.

Parameters

string $originFile The original filename
string $targetFile The target filename
bool $overwriteNewerFiles If true, target files newer than origin files are overwritten

Exceptions

FileNotFoundException When originFile doesn't exist
IOException When copy fails

mkdir(string|iterable $dirs, int $mode = 0777)

Creates a directory recursively.

Parameters

string|iterable $dirs The directory path
int $mode The directory mode

Exceptions

IOException On any directory creation failure

bool exists(string|iterable $files)

Checks the existence of files or directories.

Parameters

string|iterable $files A filename, an array of files, or a \Traversable instance to check

Return Value

bool true if the file exists, false otherwise

touch(string|iterable $files, int $time = null, int $atime = null)

Sets access and modification time of file.

Parameters

string|iterable $files A filename, an array of files, or a \Traversable instance to create
int $time The touch time as a Unix timestamp
int $atime The access time as a Unix timestamp

Exceptions

IOException When touch fails

remove(string|iterable $files)

Removes files or directories.

Parameters

string|iterable $files A filename, an array of files, or a \Traversable instance to remove

Exceptions

IOException When removal fails

chmod(string|iterable $files, int $mode, int $umask = 00, bool $recursive = false)

Change mode for an array of files or directories.

Parameters

string|iterable $files A filename, an array of files, or a \Traversable instance to change mode
int $mode The new mode (octal)
int $umask The mode mask (octal)
bool $recursive Whether change the mod recursively or not

Exceptions

IOException When the change fail

chown(string|iterable $files, string $user, bool $recursive = false)

Change the owner of an array of files or directories.

Parameters

string|iterable $files A filename, an array of files, or a \Traversable instance to change owner
string $user The new owner user name
bool $recursive Whether change the owner recursively or not

Exceptions

IOException When the change fail

chgrp(string|iterable $files, string $group, bool $recursive = false)

Change the group of an array of files or directories.

Parameters

string|iterable $files A filename, an array of files, or a \Traversable instance to change group
string $group The group name
bool $recursive Whether change the group recursively or not

Exceptions

IOException When the change fail

rename(string $origin, string $target, bool $overwrite = false)

Renames a file or a directory.

Parameters

string $origin The origin filename or directory
string $target The new filename or directory
bool $overwrite Whether to overwrite the target if it already exists

Exceptions

IOException When target file or directory already exists
IOException When origin cannot be renamed

Creates a symbolic link or copy a directory.

Parameters

string $originDir The origin directory path
string $targetDir The symbolic link name
bool $copyOnWindows Whether to copy files if on Windows

Exceptions

IOException When symlink fails

Creates a hard link, or several hard links to a file.

Parameters

string $originFile The original file
string|string[] $targetFiles The target file(s)

Exceptions

FileNotFoundException When original file is missing or not a file
IOException When link fails, including if link already exists

Resolves links in paths.

With $canonicalize = false (default) - if $path does not exist or is not a link, returns null - if $path is a link, returns the next direct target of the link without considering the existence of the target

With $canonicalize = true - if $path does not exist, returns null - if $path exists, returns its absolute fully resolved final version

Parameters

string $path A filesystem path
bool $canonicalize Whether or not to return a canonicalized path

Return Value

string|null

string makePathRelative(string $endPath, string $startPath)

Given an existing path, convert it to a path relative to a given starting path.

Parameters

string $endPath Absolute path of target
string $startPath Absolute path where traversal begins

Return Value

string Path of target relative to starting path

mirror(string $originDir, string $targetDir, Traversable $iterator = null, array $options = array())

Mirrors a directory to another.

Copies files and directories from the origin directory into the target directory. By default:

  • existing files in the target directory will be overwritten, except if they are newer (see the override option)
  • files in the target directory that do not exist in the source directory will not be deleted (see the delete option)

Parameters

string $originDir The origin directory
string $targetDir The target directory
Traversable $iterator Iterator that filters which files and directories to copy
array $options An array of boolean options Valid options are: - $options['override'] If true, target files newer than origin files are overwritten (see copy(), defaults to false) - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink(), defaults to false) - $options['delete'] Whether to delete files that are not in the source directory (defaults to false)

Exceptions

IOException When file type is unknown

bool isAbsolutePath(string $file)

Returns whether the file path is an absolute path.

Parameters

string $file A file path

Return Value

bool

string tempnam(string $dir, string $prefix)

Creates a temporary file with support for custom stream wrappers.

Parameters

string $dir The directory where the temporary filename will be created
string $prefix The prefix of the generated temporary filename Note: Windows uses only the first three characters of prefix

Return Value

string The new temporary filename (with path), or throw an exception on failure

dumpFile(string $filename, string $content)

Atomically dumps content into a file.

Parameters

string $filename The file to be written to
string $content The data to write into the file

Exceptions

IOException if the file cannot be written to

appendToFile(string $filename, string $content)

Appends content to an existing file.

Parameters

string $filename The file to which to append content
string $content The content to append

Exceptions

IOException If the file is not writable

static handleError($type, $msg)

Parameters

$type
$msg