\Symfony\Component\FilesystemFilesystem

Provides basic utility to manipulate the file system.

Summary

Methods
Properties
Constants
copy()
mkdir()
exists()
touch()
remove()
chmod()
chown()
chgrp()
rename()
symlink()
hardlink()
readlink()
makePathRelative()
mirror()
isAbsolutePath()
tempnam()
dumpFile()
appendToFile()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
isReadable()
linkException()
toIterable()
getSchemeAndHierarchy()
box()
$lastError
N/A

Properties

$lastError

$lastError : 

Type

Methods

copy()

copy(string  $originFile, string  $targetFile, boolean  $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

boolean $overwriteNewerFiles

If true, target files newer than origin files are overwritten

Throws

\Symfony\Component\Filesystem\Exception\FileNotFoundException

When originFile doesn't exist

\Symfony\Component\Filesystem\Exception\IOException

When copy fails

mkdir()

mkdir(string|\Symfony\Component\Filesystem\iterable  $dirs, integer  $mode = 511) 

Creates a directory recursively.

Parameters

string|\Symfony\Component\Filesystem\iterable $dirs

The directory path

integer $mode

The directory mode

Throws

\Symfony\Component\Filesystem\Exception\IOException

On any directory creation failure

exists()

exists(string|\Symfony\Component\Filesystem\iterable  $files) : boolean

Checks the existence of files or directories.

Parameters

string|\Symfony\Component\Filesystem\iterable $files

A filename, an array of files, or a \Traversable instance to check

Returns

boolean —

true if the file exists, false otherwise

touch()

touch(string|\Symfony\Component\Filesystem\iterable  $files, integer  $time = null, integer  $atime = null) 

Sets access and modification time of file.

Parameters

string|\Symfony\Component\Filesystem\iterable $files

A filename, an array of files, or a \Traversable instance to create

integer $time

The touch time as a Unix timestamp

integer $atime

The access time as a Unix timestamp

Throws

\Symfony\Component\Filesystem\Exception\IOException

When touch fails

remove()

remove(string|\Symfony\Component\Filesystem\iterable  $files) 

Removes files or directories.

Parameters

string|\Symfony\Component\Filesystem\iterable $files

A filename, an array of files, or a \Traversable instance to remove

Throws

\Symfony\Component\Filesystem\Exception\IOException

When removal fails

chmod()

chmod(string|\Symfony\Component\Filesystem\iterable  $files, integer  $mode, integer  $umask, boolean  $recursive = false) 

Change mode for an array of files or directories.

Parameters

string|\Symfony\Component\Filesystem\iterable $files

A filename, an array of files, or a \Traversable instance to change mode

integer $mode

The new mode (octal)

integer $umask

The mode mask (octal)

boolean $recursive

Whether change the mod recursively or not

Throws

\Symfony\Component\Filesystem\Exception\IOException

When the change fail

chown()

chown(string|\Symfony\Component\Filesystem\iterable  $files, string  $user, boolean  $recursive = false) 

Change the owner of an array of files or directories.

Parameters

string|\Symfony\Component\Filesystem\iterable $files

A filename, an array of files, or a \Traversable instance to change owner

string $user

The new owner user name

boolean $recursive

Whether change the owner recursively or not

Throws

\Symfony\Component\Filesystem\Exception\IOException

When the change fail

chgrp()

chgrp(string|\Symfony\Component\Filesystem\iterable  $files, string  $group, boolean  $recursive = false) 

Change the group of an array of files or directories.

Parameters

string|\Symfony\Component\Filesystem\iterable $files

A filename, an array of files, or a \Traversable instance to change group

string $group

The group name

boolean $recursive

Whether change the group recursively or not

Throws

\Symfony\Component\Filesystem\Exception\IOException

When the change fail

rename()

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

Renames a file or a directory.

Parameters

string $origin

The origin filename or directory

string $target

The new filename or directory

boolean $overwrite

Whether to overwrite the target if it already exists

Throws

\Symfony\Component\Filesystem\Exception\IOException

When target file or directory already exists

\Symfony\Component\Filesystem\Exception\IOException

When origin cannot be renamed

symlink()

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

Creates a symbolic link or copy a directory.

Parameters

string $originDir

The origin directory path

string $targetDir

The symbolic link name

boolean $copyOnWindows

Whether to copy files if on Windows

Throws

\Symfony\Component\Filesystem\Exception\IOException

When symlink fails

hardlink()

hardlink(string  $originFile, string|array<mixed,string>  $targetFiles) 

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

Parameters

string $originFile

The original file

string|array<mixed,string> $targetFiles

The target file(s)

Throws

\Symfony\Component\Filesystem\Exception\FileNotFoundException

When original file is missing or not a file

\Symfony\Component\Filesystem\Exception\IOException

When link fails, including if link already exists

readlink()

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

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

boolean $canonicalize

Whether or not to return a canonicalized path

Returns

string|null

makePathRelative()

makePathRelative(string  $endPath, string  $startPath) : string

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

Returns

string —

Path of target relative to starting path

mirror()

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)

Throws

\Symfony\Component\Filesystem\Exception\IOException

When file type is unknown

isAbsolutePath()

isAbsolutePath(string  $file) : boolean

Returns whether the file path is an absolute path.

Parameters

string $file

A file path

Returns

boolean

tempnam()

tempnam(string  $dir, string  $prefix) : string

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

Returns

string —

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

dumpFile()

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

Throws

\Symfony\Component\Filesystem\Exception\IOException

if the file cannot be written to

appendToFile()

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

Throws

\Symfony\Component\Filesystem\Exception\IOException

If the file is not writable

isReadable()

isReadable(string  $filename) : boolean

Tells whether a file exists and is readable.

Parameters

string $filename

Path to the file

Throws

\Symfony\Component\Filesystem\Exception\IOException

When windows path is longer than 258 characters

Returns

boolean

linkException()

linkException(string  $origin, string  $target, string  $linkType) 

Parameters

string $origin
string $target
string $linkType

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

toIterable()

toIterable(  $files) 

Parameters

$files

getSchemeAndHierarchy()

getSchemeAndHierarchy(\Symfony\Component\Filesystem\string  $filename) 

Gets a 2-tuple of scheme (may be null) and hierarchical part of a filename (e.g. file:///tmp -> array(file, tmp)).

Parameters

\Symfony\Component\Filesystem\string $filename

box()

box(  $func) 

Parameters

$func