Yii2:如何在上传图片时跳过空表格?

时间:2016-04-15 15:28:45

标签: php yii2

如果用户没有填写该表单字段,我需要跳过特定的文件上传,例如,如果用户只填写3个字段,我有4个字段来上传图像,那么控制器应该跳过第4个表单。 获取错误Call to a member function saveAs() on null,因为控制器没有跳过空表格。

如何跳过空表格?

public function actionCreate()
    {
        $model = new HomePageImg();

        if ($model->load(Yii::$app->request->post())) 
        {
            $imageName1 = $model->img_1_name;
            $imageName2 = $model->img_2_name;
            $imageName3 = $model->img_3_name;
            $imageName4 = $model->img_4_name;

            $model->file1 = UploadedFile::getInstance($model, 'file1');
            $model->file1->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName1 . '.' . $model->file1->extension);
            //save the path in the db column
            $model->photo_1 = 'uploads/hotel-home-img/' . $imageName1 . '.' . $model->file1->extension;

            $model->file2 = UploadedFile::getInstance($model, 'file2');
            $model->file2->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName2 . '.' . $model->file2->extension);
            //save the path in the db column
            $model->photo_2 = 'uploads/hotel-home-img/' . $imageName2 . '.' . $model->file1->extension;

            $model->file3 = UploadedFile::getInstance($model, 'file3');
            $model->file3->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName3 . '.' . $model->file3->extension);
            //save the path in the db column
            $model->photo_3 = 'uploads/hotel-home-img/' . $imageName3 . '.' . $model->file1->extension;

            $model->file4 = UploadedFile::getInstance($model, 'file4');
            $model->file4->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName4 . '.' . $model->file4->extension);
            //save the path in the db column
            $model->photo_4 = 'uploads/hotel-home-img/' . $imageName4 . '.' . $model->file1->extension;

            $model->save(false);

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

3 个答案:

答案 0 :(得分:2)

您可以像这样检查

if(UploadedFile::getInstance($model, 'file2')!==null)
{

}

让我帮你解决一下代码

public function actionCreate()
        {
        $model = new HomePageImg();

        if ($model->load(Yii::$app->request->post())) 
        {
            $imageName1 = $model->img_1_name;
            $imageName2 = $model->img_2_name;
            $imageName3 = $model->img_3_name;
            $imageName4 = $model->img_4_name;
            if( UploadedFile::getInstance($model, 'file1')!==null )
            {
                $model->file1 = UploadedFile::getInstance($model, 'file1');
                $model->file1->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName1 . '.' . $model->file1->extension);
                //save the path in the db column
                $model->photo_1 = 'uploads/hotel-home-img/' . $imageName1 . '.' . $model->file1->extension;
            }

            if( UploadedFile::getInstance($model, 'file2')!==null )
            {
                $model->file2 = UploadedFile::getInstance($model, 'file2');
                $model->file2->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName2 . '.' . $model->file2->extension);
                //save the path in the db column
                $model->photo_2 = 'uploads/hotel-home-img/' . $imageName2 . '.' . $model->file1->extension;
            }
            if( UploadedFile::getInstance($model, 'file3')!==null )
            {
                $model->file3 = UploadedFile::getInstance($model, 'file3');
                $model->file3->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName3 . '.' . $model->file3->extension);
                //save the path in the db column
                $model->photo_3 = 'uploads/hotel-home-img/' . $imageName3 . '.' . $model->file1->extension;
            }
            if(UploadedFile::getInstance($model, 'file4')!==null )
            {
                $model->file4 = UploadedFile::getInstance($model, 'file4');
                $model->file4->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName4 . '.' . $model->file4->extension);
                //save the path in the db column
                $model->photo_4 = 'uploads/hotel-home-img/' . $imageName4 . '.' . $model->file1->extension;
            }

            $model->save(false);

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

答案 1 :(得分:0)

在调用saveAs之前检查对象是否为空:

$model->file3 = UploadedFile::getInstance($model, 'file3');
if ($model->file3) { // is_null($model->file3) will also work
    $model->file3->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName3 . '.' . $model->file3->extension);
}

答案 2 :(得分:0)

$model->file3 = UploadedFile::getInstance($model, 'file3');
if ($model->file3 === true) {
    $model->file3->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName3 . '.' . $model->file3->extension);
}