Yii2: failed to open stream: no such file or directory

I have a problem with my upload / update function in Yii2 framework.

My update function:

    public function actionUpdate($id) {
    $model = $this->findModel($id);
    //        $model->scenario = 'update';

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        $uploadDir = Yii::getAlias('@web/web/uploads') . Yii::getAlias('@web/web/uploads');
        $uploadPath = Yii::getAlias('@web/web/uploads');

        $model->save();
        $imageProject = $model->id;

        //Main Image
        $image = UploadedFile::getInstance($model, 'main_image');

        if ($image) {
            $imgProject = 'project_' . $imageProject . '.' . $image->getExtension();
            $image->saveAs($uploadDir . '/' . $imgProject);
            $model->main_image = $uploadPath . '/' . $imgProject;
        }

        if ($model->upload()) {
            $model->save();

            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}

      

And my params.php file:

return [
'aliases' => [
    '@projectImgPath' => '@webroot/web/uploads',
    '@projectImgUrl' => '@web/web/'
]

      

];

When I upload a file to my form, I have this error:

move_uploaded_file(/web/uploads/web/uploads/gallery_1_3.png): failed to open stream: No such file or directory

      

Anyone can help me ... thanks!

+3


source to share


1 answer


 $uploadDir = Yii::getAlias('@web/web/uploads') . Yii::getAlias('@web/web/uploads');

      

replace



 $uploadDir = Yii::getAlias('@web/web/uploads');

      

0


source







All Articles