mardi 4 août 2015

How to upload two files at two different paths in Yii2?

public function actionCreate()
{
        $model = new RoomTypes();
        if ($model->load(Yii::$app->request->post())) 
        {
            $imageName_1 = $model->room_type.'_'.'1';
            $model->pic_1 = UploadedFile::getInstance($model, 'pic_1');
            $model->pic_1->saveAs('uploads/room_pics/'.$imageName_1.'.'.$model->pic_1->extension);
            $model->pic_1 = 'uploads/room_pics/'.$imageName_1.'.'.$model->pic_1->extension;

            $imageName_2 = $model->room_type.'_'.'2';
            $model->pic_2 = UploadedFile::getInstance($model, 'pic_2');
            $model->pic_2->saveAs('uploads/room_pics/'.$imageName_2.'.'.$model->pic_2->extension);
            $model->pic_2 = 'uploads/room_pics/pic2/'.$imageName_2.'.'.$model->pic_2->extension;
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        } else 
        {
            return $this->render('create', [
                'model' => $model,
            ]);
        }  
}

I'm not able to store 2nd image in folder, this code is storing 1st or 2nd image in folder, only one image is getting stored not all two, how to achive this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire