【问题标题】:Yii2 Blank page while upload file with .php extensionYii2 上传带有 .php 扩展名的文件时出现空白页面
【发布时间】:2017-06-19 08:05:07
【问题描述】:

上传带有 .php 文件扩展名的文件时遇到问题。问题是变成空白页,无法成功重定向到索引(文件已上传但页面为空白)

当我使用其他扩展文件(jpeg、jpg、txt、doc、docx 等)时,不会发生这种情况。

附言。我使用 Oracle 作为数据库并使用 yii2 UploadedFile

这是我的模型

public static function tableName()
{
    return 'JOB';
}

public function rules()
{
    return [
        [['JOBNAME', 'CLASS', 'ACTION', 'SCHEDULE', 'STATUS'], 'required'],
        [['ID', 'STATUS'], 'integer'],
        [['JOBNAME'], 'string', 'max' => 30],
        [['FILECOMMAND'], 'file', 'skipOnEmpty' => false, 'extensions' => 'jpeg, php, txt'],
        [['CLASS', 'ACTION', 'SCHEDULE'], 'string', 'max' => 100],
        [['ID'], 'unique'],
    ];
}

}

这是我的控制器

public function actionCreate()
{
    $scheduleList = yii::$app->params['cronparam'];
    $model = new JOB();
    if (yii::$app->request->post()) {
        $state = true;
        $data = yii::$app->request->post()['JOB'];
        try {
            $transaction = Yii::$app->db->beginTransaction();
            $model->JOBNAME = $data['JOBNAME'];
            $model->CLASS = $data['CLASS'];
            $model->ACTION = $data['ACTION'];
            $model->SCHEDULE = $data['SCHEDULE'];
            $model->STATUS = $data['STATUS'];
            $model->files = UploadedFile::getInstance($model, 'FILECOMMAND');
            $model->FILECOMMAND = $model->files;
            $model->files->saveAs(yii::getAlias('@app') . yii::$app->params['pathJobFile'] . $model->files->baseName . '.' . $model->files->extension, false);

            if (!$model->save()) {
                $ErrorMessage = $model->getErrorMessage($model->getErrors());
                throw new Exception($ErrorMessage);
            }
            $message = "Success insert Job " . ucwords($model->JOBNAME);
            $transaction->commit();
        } catch (Exception $e) {
            $message = $e->getMessage();
            $state = false;
            $transaction->rollBack();
        }
        if ($state) {
            Yii::$app->session->setFlash('SuccessJob', $message);
            $this->redirect('index');
        } else {
            Yii::$app->session->setFlash('ErrorJob', $message);
            $this->render('create', ['scheduleList' => $scheduleList, 'model' => $model]);
        }
    } else {
        return $this->render('create', ['scheduleList' => $scheduleList, 'model' => $model]);
    }
}

【问题讨论】:

    标签: php file upload yii2 yii2-basic-app


    【解决方案1】:

    在此处添加return 语句:

     if ($state) {
        Yii::$app->session->setFlash('SuccessJob', $message);
        return $this->redirect('index');  //here
     } else {
        Yii::$app->session->setFlash('ErrorJob', $message);
        return $this->render('create', ['scheduleList' => $scheduleList, 'model' => $model]); //and here
     }
    

    【讨论】:

    • 在我写完之后,使用php扩展文件上传时仍然出现问题,解决方案是在模型中添加'checkExtensionByMimeType'=>false
    猜你喜欢
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 2013-07-02
    • 2019-06-19
    • 2012-03-10
    • 1970-01-01
    • 2011-06-28
    相关资源
    最近更新 更多