2016-12-19 1 views
2

파일 업로드가있는 양식이 있지만 불행히도 php webshell과 같은 악의적 인 파일을 업로드 할 수 있습니다.악의적 인 파일 업로드를 방지하는 방법은 무엇입니까?

여기 내 컨트롤러 코드입니다.

public function actionIndex() 
    { 
     $model = new TbBank(); 
     $searchModel = new TbBankSearch(); 
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 

     if ($model->load(Yii::$app->request->post())) { 
      //get the instance of the uploaded file 
      //return \yii\helpers\VarDumper::dump($model); 
      $model->bank_code = Html::encode($model->bank_code); 
      $model->nama = Html::encode($model->nama); 
      $imageName = $model->bank_code; 
      $model->file = UploadedFile::getInstance($model,'file'); 
      //save the path in the db column 
      $path_image = \Yii::$app->params['uploadPath']."bank/"; 
      $model->logo = $imageName.'.'.$model->file->extension; 
      $model->save(); 
      if ($model->file->saveAs($path_image.$imageName.'.'.$model->file->extension)){ 
       return $this->redirect(['index', 'id' => $model->bank_id]); 
      }else{ 
       \yii\helpers\VarDumper::dump($model); 
      } 
     } else { 
      return $this->render('index', [ 
       'searchModel' => $searchModel, 
       'dataProvider' => $dataProvider, 
       'model' => $model, 
      ]); 
     } 


    } 

여기 내 TbBank 모델 방법은 파일을 다음 파일을 저장 안전 경우 다른 오류를 반환합니다 악성 파일이 포함 된 경우

public function rules() 
    { 
     return [ 
      [['bank_code', 'nama'], 'required'], 
      [['bank_code'], 'string', 'max' => 10], 
      [['nama'], 'string', 'max' => 50],   
      [['logo'], 'string', 'max' => 200], 
      [['file'],'file','skipOnEmpty'=>true,'extensions'=>'gif,jpg,jpeg,png','maxSize'=>100*1024*1], 
      [['file'],'required','on'=>'create'], 
     ]; 
    } 

내가 필요로하는 사용자가 제출할 때입니다, 그것은 업로드 된 파일을 검사 규칙입니다 저장 장치에 저장 한 다음 저장하십시오.

yii2와 같은 일부 인터넷 검색 결과가 악의적 인 파일을 확인하는 기능이 내장되어 있지 않습니다.

미리 감사드립니다.

답변

1

제대로 검사하지 않으면 유효성 검사 규칙이 어떻게 작동 할 것으로 기대합니까?

public function actionIndex() 
{ 
    // ... 

    if ($model->load(Yii::$app->request->post())) { 

     // ... 

     // first prepare UploadedFile instance 
     $model->file = UploadedFile::getInstance($model,'file'); 

     // THEN run validation AND IF everything is OK move on 
     if ($model->validate()) { 

      // VERIFY if save() is successful 
      if ($model->save()) { 

       // THEN try to save file 
       if ($model->file->saveAs($path_image.$imageName.'.'.$model->file->extension)){ 
        // ... 
       } 
      } 
     } 
    } 
} 
관련 문제