Yii2 UploadedFile info

Hello. How can i generate the link of the uploaded files and store it in db separated by commas.


public function actionCreate()

    {

        $model = new Consum();


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

            

            $model->file = UploadedFile::getInstances($model, 'file');

            if (!is_dir(Yii::getAlias('webroot') .'/uploads/report/'. $model->consum . '/' . date('Y-m-d'))) {

                mkdir(Yii::getAlias('webroot') . '/uploads/report/' . $model->consum . '/' . date('Y-m-d'), 0644, true);

            }


            //mkdir(Yii::getAlias('webroot') .'/uploads/report/'. $model->consum, 0644, true);

            if ($model->file && $model->validate()) {

                foreach ($model->file as $file) {

                    $file->saveAs(Yii::getAlias('webroot').'/uploads/report/' . $model->consum . '/' . date('Y-m-d') . '/' . $file->baseName . '-' . date('Y-m-d') . '-' . $model->consum . '.' . $file->extension);

                }

            }

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

        } else {

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

                'model' => $model,

            ]);

        }

    }


public function actionUpdate($id)

    {

        $model = $this->findModel($id);


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

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

        } else {

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

                'model' => $model,

            ]);

        }

    }



I’m able to upload the files correctly but i cant manage to generate the url’s for them

Hi, @misto99 ;)

Make array of names and accept it, for example, to $model->file_names = implode(’,’, $file_names);

Hello umneeq. Can u expand the reply? Thanks

Something like this:




public function actionCreate()

    {

        $model = new Consum();


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

            $file_names = [];

            

            $model->file = UploadedFile::getInstances($model, 'file');

            if (!is_dir(Yii::getAlias('webroot') .'/uploads/report/'. $model->consum . '/' . date('Y-m-d'))) {

                mkdir(Yii::getAlias('webroot') . '/uploads/report/' . $model->consum . '/' . date('Y-m-d'), 0644, true);

            }


            //mkdir(Yii::getAlias('webroot') .'/uploads/report/'. $model->consum, 0644, true);

            if ($model->file) {

                foreach ($model->file as $file) {

                    $file_name = Yii::getAlias('@webroot') . '/uploads/report/' . $model->consum . '/' . date('Y-m-d') . '/' . $file->baseName . '-' . date('Y-m-d') . '-' . $model->consum . '.' . $file->extension;

                    $file->saveAs(name);

                    $file_names[] = $file_name;

                }

                $model->file_names = implode(',', $file_names);

            }

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

        } else {

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

                'model' => $model,

            ]);

        }

    }




But better way is move this code to model

And you have to store the date information to your Consum model, otherwise you will not be able to get the paths of the saved files afterwards.

I used your method, but it gives me Call to a member function saveAs() on a non-object. the name is present in db, defined in model as string and safe, however i still get the error.