Nette Documentation Preview

syntax
Filesystem Functions
********************

.[perex]
[api:Nette\Utils\FileSystem] is a static class, which contains useful functions for working with a filesystem.

→ [Installation and requirements |@home#Installation]

Following examples assume the following class alias is defined:

```php
use Nette\Utils\FileSystem;
```


copy(string $origin, string $target, bool $overwrite=true): void .[method]
--------------------------------------------------------------------------

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 .[method]
------------------------------------------------------------

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


delete(string $path): void .[method]
------------------------------------

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


isAbsolute(string $path): bool .[method]
----------------------------------------

Determines if the `$path` is absolute.

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


joinPaths(string ...$segments): string .[method]
------------------------------------------------
Joins all segments of the path and normalizes the result.

```php
FileSystem::joinPaths('a', 'b', 'file.txt'); // 'a/b/file.txt'
FileSystem::joinPaths('/a/', '/b/');         // '/a/b/'
FileSystem::joinPaths('/a/', '/../b');       // '/b'
```


normalizePath(string $path): string .[method]
---------------------------------------------
Normalizes `..` and `.` and directory separators in path.

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


read(string $file): string .[method]
------------------------------------

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


rename(string $origin, string $target, bool $overwrite=true): void .[method]
----------------------------------------------------------------------------

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 .[method]
--------------------------------------------------------------------

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



{{composer: nette/utils}}

Filesystem Functions

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

Installation and requirements

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

joinPaths(string …$segments)string

Joins all segments of the path and normalizes the result.

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

normalizePath(string $path)string

Normalizes .. and . and directory separators in path.

FileSystem::normalizePath('/file/.');        // '/file/'
FileSystem::normalizePath('\file\..');       // '/file'
FileSystem::normalizePath('/file/../..');    // '/..'
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 $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.