Skip to content

Commit 84a9281

Browse files
committed
finished file upload guide [skip ci]
1 parent 2e0c62a commit 84a9281

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

docs/guide/input-file-upload.md

+38-32
Original file line numberDiff line numberDiff line change
@@ -116,53 +116,65 @@ the uploaded file is valid and save the file on the server.
116116

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

119-
If you need to upload multiple files at once, some adjustments are required.
120-
121-
Model:
119+
You can also upload multiple files at once, with some adjustments to the code listed in the previous subsections.
120+
121+
First you should adjust the model class by adding the `maxFiles` option in the `file` validation rule to limit
122+
the maximum number of files allowed to upload. The `upload()` method should also be updated to save the uploaded files
123+
one by one.
122124

123125
```php
126+
namespace app\models;
127+
128+
use yii\base\Model;
129+
use yii\web\UploadedFile;
130+
124131
class UploadForm extends Model
125132
{
126133
/**
127-
* @var UploadedFile|Null file attribute
134+
* @var UploadedFile[]
128135
*/
129-
public $file;
136+
public $imageFiles;
130137

131-
/**
132-
* @return array the validation rules.
133-
*/
134138
public function rules()
135139
{
136140
return [
137-
[['file'], 'file', 'maxFiles' => 10], // <--- here!
141+
[['imageFiles'], 'file', 'skipOnEmpty' => false, 'fileExtension' => 'png, jpg', 'maxFiles' => 4],
138142
];
139143
}
144+
145+
public function upload()
146+
{
147+
if ($this->validate()) {
148+
foreach ($this->imageFiles as $file) {
149+
$file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
150+
}
151+
return true;
152+
} else {
153+
return false;
154+
}
155+
}
140156
}
141157
```
142158

143-
View:
144-
159+
In the view file, you should add the `multiple` option to the `fileInput()` call so that the file upload field
160+
can receive multiple files:
161+
145162
```php
146163
<?php
147164
use yii\widgets\ActiveForm;
148-
149-
$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]);
150165
?>
151166

152-
<?= $form->field($model, 'file[]')->fileInput(['multiple' => true]) ?>
153-
154-
<button>Submit</button>
167+
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
155168

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

159-
The difference is the following line:
171+
<button>Submit</button>
160172

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

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

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

181193
if (Yii::$app->request->isPost) {
182-
$model->file = UploadedFile::getInstances($model, 'file');
183-
184-
if ($model->file && $model->validate()) {
185-
foreach ($model->file as $file) {
186-
$file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
187-
}
194+
$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
195+
if ($model->upload()) {
196+
// file is uploaded successfully
197+
return;
188198
}
189199
}
190200

191201
return $this->render('upload', ['model' => $model]);
192202
}
193203
}
194204
```
195-
196-
There are two differences from single file upload. First is that `UploadedFile::getInstances($model, 'file');` is used
197-
instead of `UploadedFile::getInstance($model, 'file');`. The former returns instances for **all** uploaded files while
198-
the latter gives you only a single instance. The second difference is that we're doing `foreach` and saving each file.

docs/internals/translation-status.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ db-mongodb.md |
5252
db-elasticsearch.md |
5353
input-forms.md |
5454
input-validation.md | Yes
55-
input-file-upload.md |
55+
input-file-upload.md | Yes
5656
input-multiple-models.md |
5757
input-tabular-input.md |
5858
output-formatting.md |

0 commit comments

Comments
 (0)