使用yii2上传文件验证

时间:2016-12-08 14:11:24

标签: php yii2

我正在尝试上传文件而我设置了规则,问题是即使我选择文件也有错误请上传文件。有什么不对,因为我注意到有一些验证规则,我已完成this

[['file'], 'file', 'skipOnEmpty' => FALSE,'on'=>'new']

在控制器处设置

 $type = new \app\models\Buildobjects();
        $type->scenario="new";

[编辑]我的控制器是

if ($type->load(Yii::$app->request->post()) && $type->validate()) {
            $type->file = UploadedFile::getInstance($type, 'file');
            $up = $this->generateRandomString(3);
            if (isset($type->file)) {
                if ($type->upload($up, $type->file)) {
                    $type->file = $up . $type->file->baseName . '.' . $type->file->extension;
                    // file is uploaded successfully
                    //return;
                }
            }
            $type->save();
            $this->redirect(Url::to(['site/archive', 'param' => $param]), 302);
        }

0 个答案:

没有答案