FileUpload上传图片保存图片失败,但没有显示错误信息

FileUpload to upload image fail to save the image, but no error message displayed

我正在使用 Yii2 基础版。看起来没什么问题,没有显示错误信息,但为什么我的图片没有上传?其余的(标题、内容等)通过表单上传,不过

这是我模型的规则和相关方法:

public $image;

 public function init(){
   Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/uploads/batam/';
   Yii::$app->params['uploadUrl'] = Yii::$app->urlManager->baseUrl . '/uploads/batam/';
    }

 public function rules()
    {
        return [
            [['title', 'content'], 'required'],
            [['content'], 'string'],
            [['created_at', 'updated_at','image'], 'safe'],
            [['image'], 'file','extensions'=>'jpg,png,jpeg'],
            [['title'], 'string', 'max' => 255],
        ];
    }

    public function getImageFile() 
    {
        return isset($this->image) ? Yii::$app->params['uploadPath'].$this->image : null;
    }

  public function uploadImage() {
     $image = UploadedFile::getInstance($this, 'image');
     if (empty($image)) {
            return false;
     }
     $this->image = $image->name;
        return $image;
     }

这是我的控制器

    public function actionCreate()
    {
        $model = new News();
        if ($model->load(Yii::$app->request->post()) )
         {
           // process uploaded image file instance                    
            $image = $model->uploadImage();
                if($model->validate())
                {
                    if($model->save())
                        { 
                        // upload only if valid uploaded file instance found
                        if ($image !== false) 
                            {
                                $path = $model->getImageFile();
                                $image->saveAs($path);
                            }
                             return $this->redirect(['view', 'id'=>$model->id]);
                        }
                }
                        else{echo " validation is failed";}
         } 
        else{   
                     return $this->render('create', [
                    'model' => $model,
                         ]);
            }
    }

这是表格

echo $form->field($model, 'image')->widget(FileInput::classname(), [
    'options' => ['accept' => 'image/*'],
    'pluginOptions' => [['previewFileType' => 'any']]
]);

我在表格的开头也包含了 enctype <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]);

此时在控制器的if ($image !== false)部分内部,$image$pathsaved-as包含一个看似正确的路径。

这是我的 $path : C:\xampp\htdocs\gbia/uploads/batam/test image 1-01.jpg 和我的 $image 也包含 object (非空)。这是我的 $image 的 var_dump :

object(yii\web\UploadedFile)#179 (5) { ["name"]=> string(19) "test image 1-01.jpg" ["tempName"]=> string(24) "C:\xampp\tmp\php3199.tmp" ["type"]=> string(10) "image/jpeg" ["size"]=> int(925184) ["error"]=> int(0) }

我认为 saveAs() 有问题,但我想不通。用谷歌搜索,查看 Whosebug 和教程,但我仍然找不到任何答案。有人可以帮忙吗?谢谢

检查您的模型,您已将 $image 声明为 class 的 public 变量,而不是数据库中的字段,如果您想将数据存储在那里,它将永远不会工作,因为临时的 public 属性 将优先于数据库列。

public $image;

所以删除这个字段(如果它也在数据库中)或者生成一个新的列名(我建议用路径名)。

[['content', 'path'], 'string'],

然后你需要存储路径,我没看到你在控制器或 class 中的什么地方这样做。我建议您在数据库中添加一个名称为 "path" 的字段,然后在控制器中这样做:

$path = $model->getImageFile();
$image->saveAs($path);
$model->path = $path . $image // You must store the full path plus the file name
$model->save(); // then you save the model again

欢迎提出任何疑问,如果您看不到灯光,我可以向您展示示例项目。