Skip to content

Commit

Permalink
finished file upload guide [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed May 30, 2015
1 parent 2e0c62a commit 84a9281
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
70 changes: 38 additions & 32 deletions docs/guide/input-file-upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,53 +116,65 @@ the uploaded file is valid and save the file on the server.

## Uploading Multiple Files <span id="uploading-multiple-files"></span>

If you need to upload multiple files at once, some adjustments are required.

Model:
You can also upload multiple files at once, with some adjustments to the code listed in the previous subsections.

First you should adjust the model class by adding the `maxFiles` option in the `file` validation rule to limit
the maximum number of files allowed to upload. The `upload()` method should also be updated to save the uploaded files
one by one.

```php
namespace app\models;

use yii\base\Model;
use yii\web\UploadedFile;

class UploadForm extends Model
{
/**
* @var UploadedFile|Null file attribute
* @var UploadedFile[]
*/
public $file;
public $imageFiles;

/**
* @return array the validation rules.
*/
public function rules()
{
return [
[['file'], 'file', 'maxFiles' => 10], // <--- here!
[['imageFiles'], 'file', 'skipOnEmpty' => false, 'fileExtension' => 'png, jpg', 'maxFiles' => 4],
];
}

public function upload()
{
if ($this->validate()) {
foreach ($this->imageFiles as $file) {
$file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
}
return true;
} else {
return false;
}
}
}
```

View:

In the view file, you should add the `multiple` option to the `fileInput()` call so that the file upload field
can receive multiple files:

```php
<?php
use yii\widgets\ActiveForm;

$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]);
?>

<?= $form->field($model, 'file[]')->fileInput(['multiple' => true]) ?>

<button>Submit</button>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>

<?php ActiveForm::end(); ?>
```
<?= $form->field($model, 'imageFiles')->fileInput(['multiple' => true]) ?>

The difference is the following line:
<button>Submit</button>

```php
<?= $form->field($model, 'file[]')->fileInput(['multiple' => true]) ?>
<?php ActiveForm::end() ?>
```

Controller:
And finally in the controller action, you should call `UploadedFile::getInstances()` instead of
`UploadedFile::getInstance()` to assign an array of `UploadedFile` instances to `UploadForm::imageFiles`.

```php
namespace app\controllers;
Expand All @@ -179,20 +191,14 @@ class SiteController extends Controller
$model = new UploadForm();

if (Yii::$app->request->isPost) {
$model->file = UploadedFile::getInstances($model, 'file');

if ($model->file && $model->validate()) {
foreach ($model->file as $file) {
$file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
}
$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
if ($model->upload()) {
// file is uploaded successfully
return;
}
}

return $this->render('upload', ['model' => $model]);
}
}
```

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.
2 changes: 1 addition & 1 deletion docs/internals/translation-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ db-mongodb.md |
db-elasticsearch.md |
input-forms.md |
input-validation.md | Yes
input-file-upload.md |
input-file-upload.md | Yes
input-multiple-models.md |
input-tabular-input.md |
output-formatting.md |
Expand Down

0 comments on commit 84a9281

Please sign in to comment.