MultipartFormDataParser
API reference › yii › web › MultipartFormDataParser
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 in the following way:
return [ 'components' => [ 'request' => [ 'parsers' => [ 'multipart/form-data' => 'yii\web\MultipartFormDataParser' ], ], // ... ], // ...];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
$_FILESvalues onRequest::getBodyParams(). Until this method is invoked,$_FILESarray 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 viatmp_namekey, will not be recognized by PHP as uploaded ones. Thus functions likeis_uploaded_file()andmove_uploaded_file()will fail on them.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$force | bool | Whether to parse raw body even for ‘POST’ request and $_FILES already populated. Default: false. Available since 2.0.13. | MultipartFormDataParser |
$uploadFileMaxCount | int | Maximum upload files count. | MultipartFormDataParser |
$uploadFileMaxSize | int | Upload file max size in bytes. | MultipartFormDataParser |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
getUploadFileMaxCount() | MultipartFormDataParser | |
getUploadFileMaxSize() | MultipartFormDataParser | |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
parse() | Parses a HTTP request body. | MultipartFormDataParser |
setUploadFileMaxCount() | MultipartFormDataParser | |
setUploadFileMaxSize() | MultipartFormDataParser |
Property details
Section titled “Property details”$force
Section titled “$force ”public bool $force = falseWhether 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
$_FILESwill be reset on each parse.
Since
2.0.13
Method details
Section titled “Method details”getUploadFileMaxCount()
Section titled “getUploadFileMaxCount() ”public function getUploadFileMaxCount(): intReturns
int: Maximum upload files count.
getUploadFileMaxSize()
Section titled “getUploadFileMaxSize() ”public function getUploadFileMaxSize(): intReturns
int: Upload file max size in bytes.
parse()
Section titled “parse() ”public function parse(mixed $rawBody, mixed $contentType): mixedParses 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()
Section titled “setUploadFileMaxCount() ”public function setUploadFileMaxCount(int $uploadFileMaxCount): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$uploadFileMaxCount | int | Maximum upload files count. |
Returns
mixed
setUploadFileMaxSize()
Section titled “setUploadFileMaxSize() ”public function setUploadFileMaxSize(int $uploadFileMaxSize): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$uploadFileMaxSize | int | Upload file max size in bytes. |
Returns
mixed