I have a problem when uploading a file with a .php file extension. The problem is becoming blank pages and unsuccessfully redirected to index (files uploaded but the page is blank)
This does not happen when I use other extension files (jpeg, jpg, txt, doc, docx etc).
Ps. I am using Oracle as database and using yii2 UploadedFile
Here my model
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'],
];
}
}
Here my controller
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]);
}
}
Add return
statements here:
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
}