### First you need to create a model that will handle the form of download the file.
```php
namespaceapp\models;
useyii\base\Model;
useyii\web\UploadedFile;
/**
* UploadForm is the model behind the upload form.
*/
classUploadFormextendsModel
{
/**
* @var UploadedFile|Null file attribute
*/
public$file;
/**
* @return array the validation rules.
*/
publicfunctionrules()
{
return[
[['file'],'file'],
];
}
}
```
In this code, we created a model ```UploadForm``` with an attribute ```$file``` that will be is ```<input type="file">``` in upload form and pointed out to him validation rule ```file```. This rule is [[yii\validators\FileValidator|FileValidator]]
It is different attribute ```'enctype' => 'multipart/form-data'``` from the standard form. This value is required when you are using forms that have a file upload control. ```fileInput()``` represents a form input field.
### Thirdly, that create the controller that will connect our form and model.
The difference here from the standard crud action, so use ```UploadedFile::getInstance(...)``` instead ```model->load(...)```. [[\yii\web\UploadedFile|UploadedFile]] does not run the model validation, it only provides information about the uploaded file. Therefore, you need to run validation manually ```$model->validate()```. This triggers the [[yii\validators\FileValidator|FileValidator]] that expects a file
If you use "basic" application then forlder ```uploads``` should be create inside ```web``` folder.
Everything is ready, now run the page and download the file. Check the folder ```basic/web/uploads``` to make sure that you have downloaded.
## Additional information.
***
### Required rule
If you need to check the mandatory download the file, then use ```skipOnEmpty```.
```php
publicfunctionrules()
{
return[
[['file'],'file','skipOnEmpty'=>false],
];
}
```
***
### Path upload folder
Folder to download the file can be installed using ```Yii::getAlias('@app/uploads')```. This base path of currently running application and folder ```uploads``
As you can see, the name of the expansion may be one and the file type - other, actually.
``UploadedFile::getInstance()->type``` also do not take this value for granted.
Instead, use [[\yii\helpers\BaseFileHelper|FileHelper]] and his [[FileHelper::getMimeType()]] to determine the exact MIME type.
If allowed to **load only the images**, using [[\yii\validators\ImageValidator|ImageValidator]] instead [[yii\validators\FileValidator|FileValidator]].
* ``` UploadedFile::getInstances($model, 'file');``` instead ``` UploadedFile::getInstance($model, 'file');```. First returns **all** uploaded files for the given model attribute, second - one.