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 $origin, string $target, bool $overwrite=true)void

Copies a file or a directory $origin to a $target. Overwrites existing files and directories by default. If $overwrite is set to false and a $target 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 if exists. Throws an exception Nette\IOException on error occurred.

isAbsolute(string $path)bool

Determines if the $path is absolute.

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

read(string $file): string

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

rename(string $origin, string $target, bool $overwrite=true)void

Renames or moves a file or a directory specified by $origin to $target. Overwrites existing files and directories by default. If $overwrite is set to false and $target 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.

version: 4.0 3.x 2.x