@@ -116,53 +116,65 @@ the uploaded file is valid and save the file on the server.
116
116
117
117
## Uploading Multiple Files <span id =" uploading-multiple-files " ></span >
118
118
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.
122
124
123
125
``` php
126
+ namespace app\models;
127
+
128
+ use yii\base\Model;
129
+ use yii\web\UploadedFile;
130
+
124
131
class UploadForm extends Model
125
132
{
126
133
/**
127
- * @var UploadedFile|Null file attribute
134
+ * @var UploadedFile[]
128
135
*/
129
- public $file ;
136
+ public $imageFiles ;
130
137
131
- /**
132
- * @return array the validation rules.
133
- */
134
138
public function rules()
135
139
{
136
140
return [
137
- [['file '], 'file', 'maxFiles ' => 10], // < --- here!
141
+ [['imageFiles '], 'file', 'skipOnEmpty ' => false, 'fileExtension' => 'png, jpg', 'maxFiles' => 4],
138
142
];
139
143
}
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
+ }
140
156
}
141
157
```
142
158
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
+
145
162
``` php
146
163
<?php
147
164
use yii\widgets\ActiveForm;
148
-
149
- $form = ActiveForm::begin([ ' options' => ['enctype' => 'multipart/form-data']]);
150
165
?>
151
166
152
- <?= $form->field($model, 'file[]')->fileInput(['multiple' => true]) ?>
153
-
154
- <button >Submit</button >
167
+ <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
155
168
156
- <?php ActiveForm::end(); ?>
157
- ```
169
+ <?= $form->field($model, 'imageFiles')->fileInput(['multiple' => true]) ?>
158
170
159
- The difference is the following line:
171
+ < button >Submit</ button >
160
172
161
- ``` php
162
- <?= $form->field($model, 'file[]')->fileInput(['multiple' => true]) ?>
173
+ <?php ActiveForm::end() ?>
163
174
```
164
175
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 ` .
166
178
167
179
``` php
168
180
namespace app\controllers;
@@ -179,20 +191,14 @@ class SiteController extends Controller
179
191
$model = new UploadForm();
180
192
181
193
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;
188
198
}
189
199
}
190
200
191
201
return $this->render('upload', ['model' => $model]);
192
202
}
193
203
}
194
204
```
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.
0 commit comments