Kartik File Upload

I have the below which works perfectly for uploading a single image. when i try to upload multiple it only uploads the first image. can anyone point me in the right direction?

view:

echo $form->field($model, 'image[]')->widget(FileInput::classname(), [
    'options'=>['accept'=>'image/*', 'multiple'=>true],
]);

Controller:

public function actionCreate()
    {
        $model = new PhotoPhotos();
        $imagine = Image::getImagine();

        if ($model->load(Yii::$app->request->post())) {

            // process uploaded image file instance
            $images = $model->uploadImage();

         if ($model->save()) {

            // upload only if valid uploaded file instance found
                if ($images !== false) {
                    $path = $model->getImageFile();
                    $paththumb = $model->getImageFileThumb();
                    $images->saveAs($path);
                   Image::thumbnail($path, 100, 100)->save(Yii::getAlias($paththumb), ['quality' => 80]);
                }

            return $this->redirect(['../article', 'id' => $model->article_id]);
        } else {

Model:

public function uploadImage() {

        // get the uploaded file instance. for multiple file uploads
        // the following data will return an array (you may need to use
        // getInstances method)
         $image = UploadedFile::getInstances($this, 'image');

         foreach ($image as $images) {

        // if no image was uploaded abort the upload
        if (empty($images)) {
            
            return false;
        }
        // store the source file name
        $this->image_src_filename = $images->name;
        $extvar = (explode(".", $images->name));
        $ext = end($extvar);
        

        // generate a unique file name
        $this->image_web_filename = Yii::$app->security->generateRandomString().".{$ext}";

        // the uploaded image instance

        return $images;

Thank you in advanced!

I have not tested your code but first error I see is that the foreach cycle in uploadImage() function does not have closing β€˜}’ therefore it processes first file and returns.

You could try to follow an approach used in answer to this question https://stackoverflow.com/questions/32217193/how-to-upload-multiple-files-using-yii-framework-2-0

1 Like

As @jaimez has pointed out, your code fails to handle the array of posted images in uploadImage method and in actionCreate method. And it seems that other parts of your PhotoPhotos model (filename, thumbnail, … etc.) is not properly designed to handle multiple images.

I believe it’s a good idea for you to start again from the basics written in the guide.:slightly_smiling_face:

Guide > Getting Data from Users > Uploading Files
https://www.yiiframework.com/doc/guide/2.0/en/input-file-upload
The guide also describes how to handle multiple files. Please check it.

Thank you both.

i will start again!

1 Like

So i started again and now hav this:
but now it will save all the files but will only save into the DB once

Model:

public function upload()
    {

        if($this->validate()) {
            foreach ($this->imageFiles as $file) {
                $this->image_src_filename = $file->name;
                $this->image_web_filename = Yii::$app->security->generateRandomString(). '.' . $file->extension;
                //$this->save(false);
                $this->save(false);
                $file->saveas('uploads/18/' . $this->image_web_filename);


            }

            return true;
        }else{
            return false;
        }
    }

Controller:

public function actionCreate()
    {

        $model = new PhotoPhotos();
        $imagine = Image::getImagine();

        if(yii::$app->request->isPost) {
            $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
            if ($model->upload()){

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

Thank you for the help guys but i have now managed to get it to work.
had to set the Model again after the for each!