上传带有.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]);
}
}
答案 0 :(得分:0)
在此处添加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
}