Nette Documentation Preview

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

.[perex]
[api:Nette\Utils\FileSystem] is a static class, which contains useful functions for working with a filesystem. One advantage over native PHP functions is that they throw exceptions in case of errors.


Installation:

```shell
composer require nette/utils
```

Following examples assume the following class alias is defined:

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


Manipulation
============


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

Copies a file or an entire directory. 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.

```php
FileSystem::copy('/path/to/source', '/path/to/dest', overwrite: true);
```


createDir(string $directory, int $mode=0777): void .[method]
------------------------------------------------------------

Creates a directory if it does not exist, including parent directories. Throws an exception `Nette\IOException` on error occurred.

```php
FileSystem::createDir('/path/to/dir');
```


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

Deletes a file or an entire directory if exists. If the directory is not empty, it deletes its contents first. Throws an exception `Nette\IOException` on error occurred.

```php
FileSystem::delete('/path/to/fileOrDir');
```


makeWritable(string $path, int $dirMode=0777, int $fileMode=0666): void .[method]
---------------------------------------------------------------------------------

Sets file permissions to `$fileMode` or directory permissions to `$dirMode`. Recursively traverses and sets permissions on the entire contents of the directory as well.

```php
FileSystem::makeWritable('/path/to/fileOrDir');
```


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

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

```php
$content = FileSystem::read('/path/to/file');
```


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.

```php
FileSystem::rename('/path/to/source', '/path/to/dest', overwrite: true);
```


write(string $file, string $content, int $mode=0666): void .[method]
--------------------------------------------------------------------

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

```php
FileSystem::write('/path/to/file', $content);
```


Paths
=====


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'
```

Filesystem Functions

Nette\Utils\FileSystem is a static class, which contains useful functions for working with a filesystem. One advantage over native PHP functions is that they throw exceptions in case of errors.

Installation:

composer require nette/utils

Following examples assume the following class alias is defined:

use Nette\Utils\FileSystem;

Manipulation

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

Copies a file or an entire directory. 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.

FileSystem::copy('/path/to/source', '/path/to/dest', overwrite: true);

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

Creates a directory if it does not exist, including parent directories. Throws an exception Nette\IOException on error occurred.

FileSystem::createDir('/path/to/dir');

delete(string $path): void

Deletes a file or an entire directory if exists. If the directory is not empty, it deletes its contents first. Throws an exception Nette\IOException on error occurred.

FileSystem::delete('/path/to/fileOrDir');

makeWritable(string $path, int $dirMode=0777, int $fileMode=0666)void

Sets file permissions to $fileMode or directory permissions to $dirMode. Recursively traverses and sets permissions on the entire contents of the directory as well.

FileSystem::makeWritable('/path/to/fileOrDir');

read(string $file): string

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

$content = FileSystem::read('/path/to/file');

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.

FileSystem::rename('/path/to/source', '/path/to/dest', overwrite: true);

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

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

FileSystem::write('/path/to/file', $content);

Paths

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'