# EncodedFile

> Represents a file submitted by the FilePond File Encode plugin as a base64 JSON payload.

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

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

Represents a file submitted by the FilePond File Encode plugin as a base64 JSON payload.

See also: <https://pqina.nl/filepond/docs/api/plugins/file-encode/>.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-metadata"></span>`$metadata` readonly constructor | `array<string, mixed>` | FilePond item metadata, such as `crop` or `poster`. | `EncodedFile` |
| <span id="property-name"></span>`$name` readonly constructor | `string` | Client file name. | `EncodedFile` |
| <span id="property-type"></span>`$type` readonly constructor | `string` | Client MIME type as reported by the browser. | `EncodedFile` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`fromArray()`](#method-fromArray) static | Creates a file from a decoded File Encode payload. | `EncodedFile` |
| [`fromInput()`](#method-fromInput) static | Creates the files submitted by one FilePond input. | `EncodedFile` |
| [`fromJson()`](#method-fromJson) static | Creates a file from the JSON string submitted by the File Encode plugin. | `EncodedFile` |
| [`getCropRectangle()`](#method-getCropRectangle) | Returns the crop rectangle in source image pixels stored by the Cropper.js editor. | `EncodedFile` |
| [`getData()`](#method-getData) | Returns the decoded binary content. | `EncodedFile` |
| [`getExtension()`](#method-getExtension) | Returns the lowercase extension of the client file name. | `EncodedFile` |
| [`getMimeType()`](#method-getMimeType) | Returns the MIME type detected from the content rather than the client-reported [`$type`](/docs/extensions/filepond/reference/file/encodedfile/index.md#property-type). | `EncodedFile` |
| [`getSize()`](#method-getSize) | Returns the content size in bytes. | `EncodedFile` |
| [`withData()`](#method-withData) | Returns a copy with different content, for example after server-side processing. | `EncodedFile` |

## Method details

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

```php
public static function fromArray(array<array-key, mixed> $payload): self
```

Creates a file from a decoded File Encode payload.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$payload` | `array<array-key, mixed>` | Payload with `name` and base64 `data`, plus optional `type` and `metadata`. |

**Returns**

`self`: File with decoded content.

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): if a key has an unexpected type or `data` is not valid base64.

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

```php
public static function fromInput(mixed $input): list<self>
```

Creates the files submitted by one FilePond input.

Accepts the raw request value: a JSON string for single inputs, a list of JSON strings or decoded payloads
for multiple inputs, and `null` or empty values when nothing was selected.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$input` | `mixed` | Request value of the FilePond input. |

**Returns**

`list<self>`: Decoded files, empty when nothing was submitted.

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): if the value or one of its entries is not a valid payload.

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

```php
public static function fromJson(string $json): self
```

Creates a file from the JSON string submitted by the File Encode plugin.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$json` | `string` | JSON object with `name`, `type`, `data`, and optional `metadata`. |

**Returns**

`self`: File with decoded content.

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): if the JSON is malformed or is not an object.

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

```php
public function getCropRectangle(): array{x: int, y: int, width: int, height: int}|null
```

Returns the crop rectangle in source image pixels stored by the Cropper.js editor.

**Returns**

`array{x: int, y: int, width: int, height: int}|null`: Rectangle, or `null` when the file was not cropped.

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

```php
public function getData(): string
```

Returns the decoded binary content.

**Returns**

`string`: File content.

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

```php
public function getExtension(): string
```

Returns the lowercase extension of the client file name.

**Returns**

`string`: Extension without the dot, or an empty string.

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

```php
public function getMimeType(): string
```

Returns the MIME type detected from the content rather than the client-reported [`$type`](/docs/extensions/filepond/reference/file/encodedfile/index.md#property-type).

**Returns**

`string`: Detected MIME type, or `application/octet-stream` when detection fails.

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

```php
public function getSize(): int
```

Returns the content size in bytes.

**Returns**

`int`: Size in bytes.

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

```php
public function withData(string $data, string|null $type = null, string|null $name = null): self
```

Returns a copy with different content, for example after server-side processing.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `string` | New binary content. |
| `$type` | `string\|null` | New client MIME type, or `null` to keep the current one. |
| `$name` | `string\|null` | New client file name, or `null` to keep the current one. |

**Returns**

`self`: Copy with the replaced values.