# FileSaver

> Writes encoded FilePond uploads to a directory with sanitized file names.

[API reference](/docs/api/index.md) › [FilePond](/docs/extensions/filepond/reference/index.md) › [file](/docs/extensions/filepond/reference/file/index.md) › FileSaver

| final readonly class | `yii2\extensions\filepond\file\FileSaver` |
| --- | --- |
| Source | [`src/file/FileSaver.php`](https://github.com/yii2-extensions/filepond/blob/main/src/file/FileSaver.php#L25) |

Writes encoded FilePond uploads to a directory with sanitized file names.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) |  | `FileSaver` |
| [`save()`](#method-save) | Writes the file and returns its absolute path, replacing a file with the same name. | `FileSaver` |
| [`saveAll()`](#method-saveAll) | Writes every file and returns their absolute paths. | `FileSaver` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(string $directory, int $directoryMode = 0775, int $fileMode = 0644)
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$directory` | `string` | Target directory, optionally as a registered Yii alias; created when missing. |
| `$directoryMode` | `int` | Permissions applied to directories created by the saver. |
| `$fileMode` | `int` | Permissions applied to written files. |

### save() <span id="method-save"></span>

```php
public function save(yii2\extensions\filepond\file\EncodedFile $file, string|null $name = null): string
```

Writes the file and returns its absolute path, replacing a file with the same name.

The extension follows the MIME type detected from the content: the client extension is kept only when it is
registered for that type, so a polyglot such as `avatar.php` holding GIF data is stored as `.gif`. The base name
is sanitized to `[A-Za-z0-9_-]`, so passing `$name` is the reliable way to control the stored file name.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$file` | [`EncodedFile`](/docs/extensions/filepond/reference/file/encodedfile/index.md) | File to write. |
| `$name` | `string\|null` | Base name without extension, or `null` to derive it from the client file name. |

**Returns**

`string`: Absolute path of the written file.

**Throws**

- [`RuntimeException`](https://www.php.net/manual/en/class.runtimeexception.php): if the directory is not writable or the file cannot be written.

### saveAll() <span id="method-saveAll"></span>

```php
public function saveAll(iterable<yii2\extensions\filepond\file\EncodedFile> $files, string|null $name = null): list<string>
```

Writes every file and returns their absolute paths.

When `$name` is given, the first file keeps it and the following ones receive a numeric suffix. Files that
would share a path within the batch, such as two uploads with the same client name, receive a `-1`, `-2`, ...
suffix instead of overwriting each other.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$files` | `iterable<`[`EncodedFile`](/docs/extensions/filepond/reference/file/encodedfile/index.md)`>` | Files to write. |
| `$name` | `string\|null` | Base name without extension, or `null` to derive it from each client file name. |

**Returns**

`list<string>`: Absolute paths of the written files.

**Throws**

- [`RuntimeException`](https://www.php.net/manual/en/class.runtimeexception.php): if the directory is not writable or a file cannot be written.