【发布时间】:2016-12-19 08:14:25
【问题描述】:
我有上传文件的表单,但不幸的是它可以上传像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 似乎没有内置功能来检查恶意文件。
提前致谢。
【问题讨论】:
标签: yii2