If you're using "basic" application template then folder `uploads` should be created under `web`.
If you're using the "basic" application template, then folder `uploads` should be created under `web`.
That's it. Load the page and try uploading. Uploads should end up in `basic/web/uploads`.
That's it. Load the page and try uploading. Uploads should end up in `basic/web/uploads`.
...
@@ -120,7 +120,7 @@ Additional information
...
@@ -120,7 +120,7 @@ Additional information
### Required rule
### Required rule
If you need to make file upload mandatory use `skipOnEmpty` like the following:
If you need to make the file upload mandatory, use `skipOnEmpty` like the following:
```php
```php
publicfunctionrules()
publicfunctionrules()
...
@@ -133,7 +133,7 @@ public function rules()
...
@@ -133,7 +133,7 @@ public function rules()
### MIME type
### MIME type
It is wise to validate type of the file uploaded. FileValidator has property `$extensions` for the purpose:
It is wise to validate the type of file uploaded. FileValidator has the property `$extensions` for this purpose:
```php
```php
publicfunctionrules()
publicfunctionrules()
...
@@ -144,8 +144,7 @@ public function rules()
...
@@ -144,8 +144,7 @@ public function rules()
}
}
```
```
The thing is that it validates only file extension and not the file content. In order to validate content as well use
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`:
`mimeTypes` property of `FileValidator`:
```php
```php
publicfunctionrules()
publicfunctionrules()
...
@@ -161,11 +160,11 @@ public function rules()
...
@@ -161,11 +160,11 @@ public function rules()
### Validating uploaded image
### Validating uploaded image
If you upload an image, [[yii\validators\ImageValidator|ImageValidator]] may come in handy. It verifies if an attribute
If you upload an image, [[yii\validators\ImageValidator|ImageValidator]] may come in handy. It verifies if an attribute
received a valid image that can be then either saved or processed using [Imagine Extension](https://github.com/yiisoft/yii2/tree/master/extensions/imagine).
received a valid image that can be then either saved or processed using the [Imagine Extension](https://github.com/yiisoft/yii2/tree/master/extensions/imagine).
### Uploading multiple files
### Uploading multiple files
If you need 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
...
@@ -243,4 +242,4 @@ class SiteController extends Controller
...
@@ -243,4 +242,4 @@ class SiteController extends Controller
```
```
The difference is `UploadedFile::getInstances($model, 'file');` instead of `UploadedFile::getInstance($model, 'file');`.
The difference is `UploadedFile::getInstances($model, 'file');` instead of `UploadedFile::getInstance($model, 'file');`.
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.