mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
Merge pull request #6543 from softark/docs-guide-input-file-upload-revised
docs/guide/input-file-upload.md - revised a bit [ci skip]
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
Uploading Files
|
||||
===============
|
||||
|
||||
Uploading files in Yii is done via the a form model, its validation rules and some controller code. Let's review what's
|
||||
Uploading files in Yii is done via a form model, its validation rules and some controller code. Let's review what's
|
||||
required to handle uploads properly.
|
||||
|
||||
|
||||
@ -39,7 +39,7 @@ class UploadForm extends Model
|
||||
}
|
||||
```
|
||||
|
||||
In the code above, we've created a model `UploadForm` with an attribute `$file` that will become `<input type="file">` in
|
||||
In the code above, we've created a model `UploadForm` with an attribute `file` that will become `<input type="file">` in
|
||||
the HTML form. The attribute has the validation rule named `file` that uses [[yii\validators\FileValidator|FileValidator]].
|
||||
|
||||
### Form view
|
||||
@ -65,7 +65,7 @@ input field.
|
||||
|
||||
### Controller
|
||||
|
||||
Now create the controller that connects the form and model together:
|
||||
Now create the controller that connects the form and the model together:
|
||||
|
||||
```php
|
||||
namespace app\controllers;
|
||||
@ -95,13 +95,16 @@ class SiteController extends Controller
|
||||
```
|
||||
|
||||
Instead of `model->load(...)`, we are using `UploadedFile::getInstance(...)`. [[\yii\web\UploadedFile|UploadedFile]]
|
||||
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:
|
||||
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]]. The validator expects that
|
||||
the attribute is an uploaded file, as you see in the core framework code:
|
||||
|
||||
```php
|
||||
$file instanceof UploadedFile || $file->error == UPLOAD_ERR_NO_FILE //in the code framework
|
||||
if (!$file instanceof UploadedFile || $file->error == UPLOAD_ERR_NO_FILE) {
|
||||
return [$this->uploadRequired, []];
|
||||
}
|
||||
```
|
||||
|
||||
If validation is successful, then we're saving the file:
|
||||
If the validation is successful, then we're saving the file:
|
||||
|
||||
```php
|
||||
$model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension);
|
||||
@ -241,6 +244,6 @@ class SiteController extends Controller
|
||||
}
|
||||
```
|
||||
|
||||
There are two differences from single file upload. First is that `UploadedFile::getInstances($model, 'file');` used
|
||||
There are two differences from single file upload. First is that `UploadedFile::getInstances($model, 'file');` is used
|
||||
instead of `UploadedFile::getInstance($model, 'file');`. The former returns instances for **all** uploaded files while
|
||||
the latter gives you only a single instance. The second difference is that we're doing `foreach` and saving each file.
|
||||
|
Reference in New Issue
Block a user