Filesystem Functions

Nette\Utils\FileSystem is a static class, which contains useful functions for working with a filesystem.

Installation:

composer require nette/utils

Following examples assume the following class alias is defined:

use Nette\Utils\FileSystem;

copy(string $source, string $destination, bool $overwrite=true)void

Copies a file or a directory $source to a $destination. Overwrites existing files and directories by default. If $overwrite is set to false and a $destination already exists, throws an exception Nette\InvalidStateException. Throws an exception Nette\IOException on error occurred.

createDir(string $directory, int $mode=0777)void

Creates a $directory if it doesn't exist. Throws an exception Nette\IOException on error occurred.

delete(string $path): void

Deletes a file or a directory specified by $path. Throws an exception Nette\IOException on error occurred.

isAbsolute(string $path)bool

Finds out wheter is a $path absolute or not.

FileSystem::isAbsolute('../backup'); // false
FileSystem::isAbsolute('/backup'); // true
FileSystem::isAbsolute('C:/backup'); // true

joinPaths(string …$paths)string

Joins all given path segments then normalizes the resulting path.

echo FileSystem::joinPaths('a', 'b'); // 'a/b'
echo FileSystem::joinPaths('/a/', '/b/'); // '/a/b/'
echo FileSystem::joinPaths('/a/', '/../'); // '/'

normalizePath(string $path)string

Normalizes .. and . and directory separators in path.

echo FileSystem::normalizePath('/file/.'); // '/file/'
echo FileSystem::normalizePath('\file\..'); // '/file'
echo FileSystem::normalizePath('/file/../..'); // '/..'
echo FileSystem::normalizePath('file/../../bar'); // '/../bar'

read(string $file): string

Reads the content of a $file. Throws an exception Nette\IOException on error occurred.

rename(string $name, string $newName, bool $overwrite=true)void

Renames or moves a file or a directory specified by $name to $newName. Overwrites existing files and directories by default. If $overwrite is set to false and $newName already exists, throws an exception Nette\InvalidStateException. Throws an exception Nette\IOException on error occurred.

write(string $file, string $content, int $mode=0666)void

Writes the $content to a $file. Throws an exception Nette\IOException on error occurred.