我在Yii2框架中的上传/更新功能有问题。
我的更新功能:
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,
]);
}
}
我的params.php文件:
return [
'aliases' => [
'@projectImgPath' => '@webroot/web/uploads',
'@projectImgUrl' => '@web/web/'
]
];
当我在表单中上传文件时出现此错误:
move_uploaded_file(/web/uploads/web/uploads/gallery_1_3.png): failed to open stream: No such file or directory
任何人都可以帮助我......谢谢!
答案 0 :(得分:0)
$uploadDir = Yii::getAlias('@web/web/uploads') . Yii::getAlias('@web/web/uploads');
替换为
$uploadDir = Yii::getAlias('@web/web/uploads');