0

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 .

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

still i faced an issue with save images, i didn't find in folder
update your question with new code (your view, model and controller code), so we can review and also check for the folder permissions.
sorry, it's was a bug from my old code, thanks a lot, it's working perfectly
glad to help you :)
0
$files = \yii\web\UploadedFile::getInstancesByName('file');

returns array of files sent with input field named "file".

1 Comment

i could not find any thing in folder either in db .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.