0

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]);
    }
}

1 Answer 1

0

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
 }
Sign up to request clarification or add additional context in comments.

1 Comment

after i write that up, problem still comes when uploading with php extension file, the solution is adding 'checkExtensionByMimeType'=>false in model

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.