Reputation: 265
i want upload multi images from html field, here is view code :
<input type="file" name="file[]" multiple id="file"/>
and my problem is in controller , as every yii2's developer knows these codes for upload file in simple way :
$model->logoFile = UploadedFile::getInstance($model, 'logoFile');
$model->logoFile->saveAs('uploads/' . $model->logoFile->name);
$model->logoImage = $model->logoFile->name;
but in my case the images did not come from yii fields liks this one :
<?= $form->field($model, 'image')->fileInput() ?>
so any idea how can i fix my controller to be get multi images and save if in server as well
Note : my tamplate is advanced .
Upvotes: 0
Views: 2303
Reputation: 1092
Note : inside your view, you are using plain html file upload, with name file
I will suggest , to change it in yii2 way like this
<?= $form->field($model, 'logoFile[]')->fileInput(['multiple' => true, 'class' => 'btn btn-default '])->label("Attachment"); ?>
also take care of variable used inside view and model
Model
//public $logoFile;
public function rules() {
return [
[['logoFile'], 'file', 'maxFiles' => 4],
]
}
Controller
$model->logoFile = \yii\web\UploadedFile::getInstances($model, 'logoFile');
foreach ($model->logoFile as $file) {
$file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
}
Upvotes: 1
Reputation: 18021
$files = \yii\web\UploadedFile::getInstancesByName('file');
returns array of files sent with input field named "file".
Upvotes: 0