Aleksandur Atanasov
Aleksandur Atanasov

Reputation: 141

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

i have 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 file in 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... thank`s !

Upvotes: 3

Views: 9724

Answers (1)

zakrzu
zakrzu

Reputation: 595

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

replace with

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

Upvotes: 1

Related Questions