Documentation

Filesystem
in package

Provides basic utility to manipulate the file system.

Tags
author

Fabien Potencier fabien@symfony.com

Table of Contents

Properties

$lastError  : string|null

Methods

appendToFile()  : void
Appends content to an existing file.
chgrp()  : void
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()  : void
Copies a file.
dumpFile()  : void
Atomically dumps content into a file.
exists()  : bool
Checks the existence of files or directories.
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
Creates a directory recursively.
readlink()  : string|null
Resolves links in paths.
remove()  : void
Removes files or directories.
rename()  : void
Renames a file or a directory.
symlink()  : void
Creates a symbolic link or copy a directory.
tempnam()  : string
Creates a temporary file with support for custom stream wrappers.
touch()  : void
Sets access and modification time of file.
assertFunctionExists()  : void
box()  : mixed
doRemove()  : void
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>

Properties

$lastError

private static string|null $lastError = null

Methods

appendToFile()

Appends content to an existing file.

public appendToFile(string $filename, string|resource $content) : void
Parameters
$filename : string
$content : string|resource

The content to append

Tags
throws
IOException

If the file is not writable

chgrp()

Change the group of an array of files or directories.

public chgrp(string|iterable<string|int, mixed> $files, string|int $group[, bool $recursive = false ]) : void

This method always throws on Windows, as the underlying PHP function is not supported.

Parameters
$files : string|iterable<string|int, mixed>
$group : string|int

A group name or number

$recursive : bool = false

Whether change the group recursively or not

Tags
see
https://www.php.net/chgrp
throws
IOException

When the change fails

chmod()

Change mode for an array of files or directories.

public chmod(string|iterable<string|int, mixed> $files, int $mode[, int $umask = 00 ][, bool $recursive = false ]) : void
Parameters
$files : string|iterable<string|int, mixed>
$mode : int

The new mode (octal)

$umask : int = 00

The mode mask (octal)

$recursive : bool = false

Whether change the mod recursively or not

Tags
throws
IOException

When the change fails

chown()

Change the owner of an array of files or directories.

public chown(string|iterable<string|int, mixed> $files, string|int $user[, bool $recursive = false ]) : void

This method always throws on Windows, as the underlying PHP function is not supported.

Parameters
$files : string|iterable<string|int, mixed>
$user : string|int

A user name or number

$recursive : bool = false

Whether change the owner recursively or not

Tags
see
https://www.php.net/chown
throws
IOException

When the change fails

copy()

Copies a file.

public copy(string $originFile, string $targetFile[, bool $overwriteNewerFiles = false ]) : void

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
$originFile : string
$targetFile : string
$overwriteNewerFiles : bool = false
Tags
throws
FileNotFoundException

When originFile doesn't exist

throws
IOException

When copy fails

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
throws
IOException

if the file cannot be written to

exists()

Checks the existence of files or directories.

public exists(string|iterable<string|int, mixed> $files) : bool
Parameters
$files : string|iterable<string|int, mixed>
Return values
bool

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

public hardlink(string $originFile, string|array<string|int, string> $targetFiles) : void
Parameters
$originFile : string
$targetFiles : string|array<string|int, string>

The target file(s)

Tags
throws
FileNotFoundException

When original file is missing or not a file

throws
IOException

When link fails, including if link already exists

isAbsolutePath()

Returns whether the file path is an absolute path.

public isAbsolutePath(string $file) : bool
Parameters
$file : string
Return values
bool

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
$startPath : string
Return values
string

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 override option)
  • files in the target directory that do not exist in the source directory will not be deleted (see the delete option)
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
throws
IOException

When file type is unknown

mkdir()

Creates a directory recursively.

public mkdir(string|iterable<string|int, mixed> $dirs[, int $mode = 0777 ]) : void
Parameters
$dirs : string|iterable<string|int, mixed>
$mode : int = 0777
Tags
throws
IOException

On any directory creation failure

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
$canonicalize : bool = false
Return values
string|null

remove()

Removes files or directories.

public remove(string|iterable<string|int, mixed> $files) : void
Parameters
$files : string|iterable<string|int, mixed>
Tags
throws
IOException

When removal fails

rename()

Renames a file or a directory.

public rename(string $origin, string $target[, bool $overwrite = false ]) : void
Parameters
$origin : string
$target : string
$overwrite : bool = false
Tags
throws
IOException

When target file or directory already exists

throws
IOException

When origin cannot be renamed

Creates a symbolic link or copy a directory.

public symlink(string $originDir, string $targetDir[, bool $copyOnWindows = false ]) : void
Parameters
$originDir : string
$targetDir : string
$copyOnWindows : bool = false
Tags
throws
IOException

When symlink fails

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 access and modification time of file.

public touch(string|iterable<string|int, mixed> $files[, int|null $time = null ][, int|null $atime = null ]) : void
Parameters
$files : string|iterable<string|int, mixed>
$time : int|null = null

The touch time as a Unix timestamp, if not supplied the current system time is used

$atime : int|null = null

The access time as a Unix timestamp, if not supplied the current system time is used

Tags
throws
IOException

When touch fails

assertFunctionExists()

private static assertFunctionExists(string $func) : void
Parameters
$func : string

box()

private static box(string $func, mixed ...$args) : mixed
Parameters
$func : string
$args : mixed

doRemove()

private static doRemove(array<string|int, mixed> $files, bool $isRecursive) : void
Parameters
$files : array<string|int, mixed>
$isRecursive : bool

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
Tags
throws
IOException

When windows path is longer than 258 characters

Return values
bool

linkException()

private linkException(string $origin, string $target, string $linkType) : never
Parameters
$origin : string
$target : string
$linkType : string

Name of the link type, typically 'symbolic' or 'hard'

Return values
never

toIterable()

private toIterable(string|iterable<string|int, mixed> $files) : iterable<string|int, mixed>
Parameters
$files : string|iterable<string|int, mixed>
Return values
iterable<string|int, mixed>

        
On this page

Search results