The `'enctype' => 'multipart/form-data'` is important since it allows file uploads. `fileInput()` represents a form
The `'enctype' => 'multipart/form-data'` is necessary because it allows file uploads. `fileInput()` represents a form
input field.
input field.
Controller
Controller
...
@@ -96,10 +96,8 @@ class SiteController extends Controller
...
@@ -96,10 +96,8 @@ class SiteController extends Controller
}
}
```
```
Instead of `model->load(...)` we are using `UploadedFile::getInstance(...)`. [[\yii\web\UploadedFile|UploadedFile]]
Instead of `model->load(...)`, we are using `UploadedFile::getInstance(...)`. [[\yii\web\UploadedFile|UploadedFile]]
does not run the model validation. It only provides information about the uploaded file. Therefore, you need to run
does not run the model validation, rather it only provides information about the uploaded file. Therefore, you need to run the validation manually via `$model->validate()` to trigger the [[yii\validators\FileValidator|FileValidator]] that expects a file:
validation manually via `$model->validate()`. This triggers the [[yii\validators\FileValidator|FileValidator]] that
expects a file:
```php
```php
$fileinstanceofUploadedFile||$file->error==UPLOAD_ERR_NO_FILE//in the code framework
$fileinstanceofUploadedFile||$file->error==UPLOAD_ERR_NO_FILE//in the code framework
...
@@ -144,7 +142,7 @@ public function rules()
...
@@ -144,7 +142,7 @@ public function rules()
}
}
```
```
Keep in mind that only the file extension will be validated, but not the actual file content. In order to validate content as well use the `mimeTypes` property of `FileValidator`:
Keep in mind that only the file extension will be validated, but not the actual file content. In order to validate the content as well, use the `mimeTypes` property of `FileValidator`:
```php
```php
publicfunctionrules()
publicfunctionrules()
...
@@ -164,7 +162,9 @@ received a valid image that can be then either saved or processed using the [Ima
...
@@ -164,7 +162,9 @@ received a valid image that can be then either saved or processed using the [Ima
### Uploading multiple files
### Uploading multiple files
If you need to download multiple files at once some adjustments are required. View:
If you need to download multiple files at once, some adjustments are required.
View:
```php
```php
<?php
<?php
...
@@ -241,5 +241,5 @@ class SiteController extends Controller
...
@@ -241,5 +241,5 @@ class SiteController extends Controller
}
}
```
```
The difference is `UploadedFile::getInstances($model, 'file');` instead of `UploadedFile::getInstance($model, 'file');`.
The difference is using `UploadedFile::getInstances($model, 'file');` instead of `UploadedFile::getInstance($model, 'file');`.
The former returns instances for **all** uploaded files while the latter gives you only a single instance.
The former returns instances for **all** uploaded files while the latter gives you only a single instance.