[Solved] Multi File Upload

Hi, I’m trying to upload multiple files, but I always get “Trying to get property of non-object”

This is my controller code:




    public function actionCreate()

    {

        $model = new Property;

        

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

             $images = \yii\web\UploadedFile::getInstance($model, 'image');

             if (isset($images) && count($images) > 0) {

                foreach ($images as $image => $pic) {

                     echo $pic->name.'<br />';

                     $ext = end((explode(".", $pic->name)));

                     $pic->name = Security::generateRandomKey().".{$ext}";

                      if ($pic->saveAs(Yii::getAlias('@imagenesInmuebles').'/'.$model->id.'/'.$pic->name)) {

                          $pic_add = new \app\models\PropertyPictures;

                          $pic_add->filename = $pic->name;

                          $pic_add->property_id = $model->id;

                          $pic_add->save();

                      } else {

                          echo 'Error al cargar imágenes';

                      }

                }

            }

            if ($model->save()){

                 return $this->redirect(['view', 'id'=>$model->id]);

             }

        } else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }



I get the error in the


echo $pic->name.'<br />';

line.

The $_FILES has this output in the error page.




$_FILES = array (

  'Property' => 

  array (

    'name' => 

    array (

      'image' => 'entrada.jpg',

    ),

    'type' => 

    array (

      'image' => 'image/jpeg',

    ),

    'tmp_name' => 

    array (

      'image' => 'C:\\xampp\\tmp\\phpCB26.tmp',

    ),

    'error' => 

    array (

      'image' => 0,

    ),

    'size' => 

    array (

      'image' => 295557,

    ),

  ),

);



Any help would be really appreciated.

Thank you.

Have you tried to make var_dump of $images?

And you should try to use getInstances instead of getInstance!

traceback is useful. So post full error trace!

This seemed to solve the issue I had, it started working with the getInstances. Thank you!!

I used getInstances as suggested by xView and it seemed to solved my issue.

Here is the Controller code. I hope it helps someone.




public function actionCreate()

    {

        $model = new Property;

        

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

            //d-m-Y to Y-m-d to store values

            $fecha = date('Y-m-d', strtotime($model->collectDate));

            $model->collectDate = $fecha;


            if($model->save()){

                        

                //Save Pics

                $images = \yii\web\UploadedFile::getInstances($model, 'image');

                    if (isset($images) && count($images) > 0) {

                    foreach ($images as $image => $pic) {

                        echo $pic->name.'<br />';

                        $ext = end((explode(".", $pic->name)));

                        //Rename the image to store it

                        $pic->name = Security::generateRandomKey().".{$ext}";

                        //Checks if folder exists for saving the pictures in a folder with the Property model id

                        $model->getCheckFolder($model->id);

                        if ($pic->saveAs(Yii::getAlias('@imagenesInmuebles').'/'.$model->id.'/'.$pic->name)) {

                            $pic_add = new \app\models\PropertyPictures;

                            $pic_add->filename = $pic->name;

                            $pic_add->url = $pic->name;

                            $pic_add->property_id = $model->id;

                            $pic_add->save();

                            \Yii::$app->session->setFlash('success',  \Yii::t('app','Image saved successfully'));

                        } else {

                            echo 'Error';

                        }

                    }

                }

                Yii::$app->session->setFlash('success',Yii::t('app','Property saved successfuly'));

                return $this->redirect(['view', 'id' => $model->id]);

            }} else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }



Thank you for your help.