# MultipartFormDataParser

> MultipartFormDataParser parses content encoded as 'multipart/form-data'.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [web](/docs/api/yii/web/index.md) › MultipartFormDataParser

| class | `yii\web\MultipartFormDataParser` |
| --- | --- |
| Extends | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`RequestParserInterface`](/docs/api/yii/web/requestparserinterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0.10 |
| Source | [`framework/web/MultipartFormDataParser.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/MultipartFormDataParser.php#L66) |

MultipartFormDataParser parses content encoded as 'multipart/form-data'.

This parser provides the fallback for the 'multipart/form-data' processing on non POST requests,
for example: the one with 'PUT' request method.

In order to enable this parser you should configure [`Request::$parsers`](/docs/api/yii/web/request/index.md#property-parsers) in the following way:

```
return [
    'components' => [
        'request' => [
            'parsers' => [
                'multipart/form-data' => 'yii\web\MultipartFormDataParser'
            ],
        ],
        // ...
    ],
    // ...
];
```

Method [`parse()`](/docs/api/yii/web/multipartformdataparser/index.md#method-parse) of this parser automatically populates `$_FILES` with the files parsed from raw body.

> Note: since this is a request parser, it will initialize `$_FILES` values on [`Request::getBodyParams()`](/docs/api/yii/web/request/index.md#method-getBodyParams).
Until this method is invoked, `$_FILES` array will remain empty even if there are submitted files in the
request body. Make sure you have requested body params before any attempt to get uploaded file in case
you are using this parser.

Usage example:

```
use yii\web\UploadedFile;

$restRequestData = Yii::$app->request->getBodyParams();
$uploadedFile = UploadedFile::getInstancesByName('photo');

$model = new Item();
$model->populate($restRequestData);
copy($uploadedFile->tempName, '/path/to/file/storage/photo.jpg');
```

> Note: although this parser fully emulates regular structure of the `$_FILES`, related temporary
files, which are available via `tmp_name` key, will not be recognized by PHP as uploaded ones.
Thus functions like `is_uploaded_file()` and `move_uploaded_file()` will fail on them.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$force`](#property-force) | `bool` | Whether to parse raw body even for 'POST' request and `$_FILES` already populated. Default: `false`. Available since 2.0.13. | `MultipartFormDataParser` |
| <span id="property-uploadFileMaxCount"></span>`$uploadFileMaxCount` | `int` | Maximum upload files count. | `MultipartFormDataParser` |
| <span id="property-uploadFileMaxSize"></span>`$uploadFileMaxSize` | `int` | Upload file max size in bytes. | `MultipartFormDataParser` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`getUploadFileMaxCount()`](#method-getUploadFileMaxCount) |  | `MultipartFormDataParser` |
| [`getUploadFileMaxSize()`](#method-getUploadFileMaxSize) |  | `MultipartFormDataParser` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`parse()`](#method-parse) | Parses a HTTP request body. | `MultipartFormDataParser` |
| [`setUploadFileMaxCount()`](#method-setUploadFileMaxCount) |  | `MultipartFormDataParser` |
| [`setUploadFileMaxSize()`](#method-setUploadFileMaxSize) |  | `MultipartFormDataParser` |

## Property details

### $force <span id="property-force"></span>

```php
public bool $force = false
```

Whether to parse raw body even for 'POST' request and `$_FILES` already populated.

By default this option is disabled saving performance for 'POST' requests, which are already
processed by PHP automatically.
> Note: if this option is enabled, value of `$_FILES` will be reset on each parse.

**Since**

2.0.13

## Method details

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

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

**Returns**

`int`: Maximum upload files count.

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

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

**Returns**

`int`: Upload file max size in bytes.

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

```php
public function parse(mixed $rawBody, mixed $contentType): mixed
```

Parses a HTTP request body.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$rawBody` | `mixed` | The raw HTTP request body. |
| `$contentType` | `mixed` | The content type specified for the request body. |

**Returns**

`mixed`: Parameters parsed from the request body

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

```php
public function setUploadFileMaxCount(int $uploadFileMaxCount): mixed
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$uploadFileMaxCount` | `int` | Maximum upload files count. |

**Returns**

`mixed`

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

```php
public function setUploadFileMaxSize(int $uploadFileMaxSize): mixed
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$uploadFileMaxSize` | `int` | Upload file max size in bytes. |

**Returns**

`mixed`