FileSystem
in package
FileSystem class
This class is responsible for managing the file system.
It provides methods to copy, move, and delete files and directories.
It also provides methods to check if a file or directory exists, and to get the contents of a file or directory.
Tags
Table of Contents
Properties
- $lastError : mixed
Methods
- appendToFile() : void
- Appends content to an existing file.
- chgrp() : mixed
- Change the group of an array of files or directories.
- chmod() : void
- Change mode for an array of files or directories.
- chown() : void
- Change the owner of an array of files or directories.
- copy() : bool
- Copy a file from the origin to the target.
- dumpFile() : void
- Atomically dumps content into a file.
- exists() : bool
- Check if one or more files exist.
- hardlink() : void
- Creates a hard link, or several hard links to a file.
- isAbsolutePath() : bool
- Returns whether the file path is an absolute path.
- makePathRelative() : string
- Given an existing path, convert it to a path relative to a given starting path.
- mirror() : void
- Mirrors a directory to another.
- mkdir() : void
- Create one or more directories.
- normalize() : string
- Normalizes a path string.
- readlink() : string|null
- Resolves links in paths.
- remove() : void
- Removes files or directories.
- rename() : mixed
- Renames a file or a directory.
- symlink() : bool
- Creates a symbolic link or copy a directory.
- tempnam() : string
- Creates a temporary file with support for custom stream wrappers.
- touch() : void
- Sets the access and modification time of a file.
- assertFunctionExists() : void
- Asserts that a function exists.
- box() : mixed
- doRemove() : void
- Removes files or directories.
- getSchemeAndHierarchy() : array<string|int, mixed>
- Gets a 2-tuple of scheme (may be null) and hierarchical part of a filename (e.g. file:///tmp -> [file, tmp]).
- isReadable() : bool
- Tells whether a file exists and is readable.
- linkException() : never
- toIterable() : iterable<string|int, mixed>
- Converts a string or iterable of strings into an array.
Properties
$lastError
private
static mixed
$lastError
Methods
appendToFile()
Appends content to an existing file.
public
appendToFile(string $filename, string|resource $content[, bool $lock = false ]) : void
Parameters
- $filename : string
- $content : string|resource
-
The content to append
- $lock : bool = false
-
Whether the file should be locked when writing to it
Tags
chgrp()
Change the group of an array of files or directories.
public
chgrp(string|int|array{string|int} $files, string|int $group[, bool $recursive = false ]) : mixed
Parameters
- $files : string|int|array{string|int}
-
A file, an array of files, or a callable returning an iterable of files
- $group : string|int
-
A group name or number
- $recursive : bool = false
-
Whether change the group recursively or not
Tags
chmod()
Change mode for an array of files or directories.
public
chmod(string|array{string}|callable $files, int $mode[, int $umask = 00 ][, bool $recursive = false ]) : void
Parameters
- $files : string|array{string}|callable
-
A file, an array of files, or a callable returning an iterable of files
- $mode : int
-
The new mode (octal)
- $umask : int = 00
-
The mode mask (octal)
- $recursive : bool = false
-
Whether change the mod recursively or not
Tags
chown()
Change the owner of an array of files or directories.
public
chown(string|array{string|int}|callable $files, string|int $user[, bool $recursive = false ]) : void
Parameters
- $files : string|array{string|int}|callable
-
A file, an array of files, or a callable returning an iterable of files
- $user : string|int
-
A user name or number
- $recursive : bool = false
-
Whether change the owner recursively or not
Tags
copy()
Copy a file from the origin to the target.
public
copy(string $originFile, string $targetFile[, bool $overwriteNewerFiles = false ]) : bool
Parameters
- $originFile : string
-
The origin file path.
- $targetFile : string
-
The target file path.
- $overwriteNewerFiles : bool = false
-
Whether to overwrite newer files.
Tags
Return values
bool —True if the file was successfully copied, false otherwise.
dumpFile()
Atomically dumps content into a file.
public
dumpFile(string $filename, string|resource $content) : void
Parameters
- $filename : string
- $content : string|resource
-
The data to write into the file
Tags
exists()
Check if one or more files exist.
public
exists(string|iterable<string|int, mixed> $files) : bool
Parameters
- $files : string|iterable<string|int, mixed>
-
The files to check for existence.
Tags
Return values
bool —True if all files exist, false otherwise.
hardlink()
Creates a hard link, or several hard links to a file.
public
hardlink(string|array<string|int, string> $originFile, string|array<string|int, string> $targetFiles) : void
Parameters
- $originFile : string|array<string|int, string>
-
origin File
- $targetFiles : string|array<string|int, string>
-
The target file(s)
Tags
isAbsolutePath()
Returns whether the file path is an absolute path.
public
isAbsolutePath(string $file) : bool
Checks if the given string is an absolute path.
This function checks if the given string is an absolute path by applying the following rules:
- The string should not be empty.
- The string should start with either a forward or backward slash.
- The string should start with a non-alphabetic character followed by a colon and then either a forward or backward slash.
- The string should be a valid URL with a scheme.
Parameters
- $file : string
-
The path to check.
Return values
bool —Returns true if the string is an absolute path, false otherwise.
makePathRelative()
Given an existing path, convert it to a path relative to a given starting path.
public
makePathRelative(string $endPath, string $startPath) : string
Parameters
- $endPath : string
-
The path to be made relative
- $startPath : string
-
The starting path
Tags
Return values
string —The relative path
mirror()
Mirrors a directory to another.
public
mirror(string $originDir, string $targetDir[, Traversable|null $iterator = null ][, array<string|int, mixed> $options = [] ]) : void
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
overrideoption) - files in the target directory that do not exist in the source
directory will not be deleted (see the
deleteoption)
Parameters
- $originDir : string
- $targetDir : string
- $iterator : Traversable|null = null
-
Iterator that filters which files and directories to copy, if null a recursive iterator is created
- $options : array<string|int, mixed> = []
-
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)
Tags
mkdir()
Create one or more directories.
public
mkdir(string|iterable<string|int, mixed> $dirs[, int $mode = 0777 ]) : void
Parameters
- $dirs : string|iterable<string|int, mixed>
-
The directories to create.
- $mode : int = 0777
-
The permissions to apply to the directories.
Tags
normalize()
Normalizes a path string.
public
static normalize(string $path) : string
Replaces backslashes with directory separators to ensure a consistent path format.
Parameters
- $path : string
-
The path to normalize
Return values
string —The normalized path
readlink()
Resolves links in paths.
public
readlink(string $path[, bool $canonicalize = false ]) : string|null
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
- $path : string
-
The path to the symbolic link
- $canonicalize : bool = false
-
Whether to return the canonicalized form of the target path (default: false)
Return values
string|null —The target path of the symbolic link, or null if the path does not exist or is not a symbolic link
remove()
Removes files or directories.
public
remove(string|array<string|int, string>|callable $files) : void
Parameters
- $files : string|array<string|int, string>|callable
-
The file, an array of files, or a callable returning an iterable of files
Tags
rename()
Renames a file or a directory.
public
rename(string $origin, string $target[, bool $overwrite = false ]) : mixed
Parameters
- $origin : string
-
The original file or directory name
- $target : string
-
The new file or directory name
- $overwrite : bool = false
-
Whether to overwrite an existing file or directory
Tags
symlink()
Creates a symbolic link or copy a directory.
public
symlink(string $originDir, string $targetDir[, bool $copyOnWindows = false ]) : bool
Parameters
- $originDir : string
-
The path of the original file or directory
- $targetDir : string
-
The path where the symbolic link should be created
- $copyOnWindows : bool = false
-
Whether to copy the file on Windows instead of creating a symlink (default: false)
Tags
Return values
bool —True if the symbolic link was created successfully, false otherwise
tempnam()
Creates a temporary file with support for custom stream wrappers.
public
tempnam(string $dir, string $prefix[, string $suffix = '' ]) : string
Parameters
- $dir : string
- $prefix : string
-
The prefix of the generated temporary filename Note: Windows uses only the first three characters of prefix
- $suffix : string = ''
-
The suffix of the generated temporary filename
Return values
string —The new temporary filename (with path), or throw an exception on failure
touch()
Sets the access and modification time of a file.
public
touch(string|iterable<string|int, mixed> $files[, int|null $time = null ][, int|null $atime = null ]) : void
This is a wrapper around the touch() PHP function.
Parameters
- $files : string|iterable<string|int, mixed>
-
The file(s) to touch.
- $time : int|null = null
-
The time. If null, the current time is used.
- $atime : int|null = null
-
The access time. If null, the modification time is used.
Tags
assertFunctionExists()
Asserts that a function exists.
private
static assertFunctionExists(string $func) : void
@throws Exception/IOGenerativeException if the function does not exist
Parameters
- $func : string
box()
private
static box(string $func, mixed ...$args) : mixed
Parameters
- $func : string
-
The name of the function to call
- $args : mixed
-
Arguments to pass to the function
Return values
mixed —The result of the function call
doRemove()
Removes files or directories.
private
static doRemove(array<string|int, mixed> $files, bool $isRecursive) : void
This is a helper function for remove() and removeRecursive().
Parameters
- $files : array<string|int, mixed>
-
The files or directories to remove.
- $isRecursive : bool
-
Whether to remove files and directories recursively or not.
Tags
getSchemeAndHierarchy()
Gets a 2-tuple of scheme (may be null) and hierarchical part of a filename (e.g. file:///tmp -> [file, tmp]).
private
getSchemeAndHierarchy(string $filename) : array<string|int, mixed>
Parameters
- $filename : string
Return values
array<string|int, mixed>isReadable()
Tells whether a file exists and is readable.
private
isReadable(string $filename) : bool
Parameters
- $filename : string
-
The name of the file to check.
Tags
Return values
boollinkException()
private
linkException(string $origin, string $target, string $linkType) : never
Parameters
- $origin : string
-
The path of the original file or directory
- $target : string
-
The path of the target file or directory
- $linkType : string
-
The type of link being created (e.g., "hard link", "symbolic link")
Tags
Return values
nevertoIterable()
Converts a string or iterable of strings into an array.
private
toIterable(string|iterable<string|int, mixed> $files) : iterable<string|int, mixed>
Parameters
- $files : string|iterable<string|int, mixed>
-
The files to convert.
Return values
iterable<string|int, mixed> —The converted iterable.