在这种情况下,我希望安全上传pdf, doc, docx, ppt, pptx, xls, xlsx, rar, zip,防止任意文件上传,特别是web或任何恶意脚本。
问题是我怎样才能验证文件,上传安全吗?防止使用篡改数据更改mime类型、重命名多扩展名文件、使用;和文件名中的空格、小写和大写文件扩展名等。
我的控制器代码如下所示
public function fileUpload(){
$ext = ['pdf', 'doc', 'ppt', 'xls', 'docx', 'pptx', 'xlsx', 'rar', 'zip'];
$data = Request::all();
$name = $data['file']->getClientOriginalName();
$rules = [
'file' => 'required'
];
$v = Validator::make($data, $rules);
if($v->passes()){
// Check safe file validation
// should here or something? and how to prevent bypass
// arbitrary file upload especially evil script.
$data['file']->move(public_path('assets/uploads'), $name);
return 'file uploaded';
}else{
return 'file upload failed';
}}
发布于 2015-10-13 15:15:24
我建议查看Laravel中间件进行验证。这将减少控制器中的代码,并允许重用它们。
我个人将任何文件上传的名称更改为随机的。如果需要的话,我总是可以在系统的某个地方保存原始文件名。
我还将考虑使用htaccess命令来防止文件从该文件夹执行。
控制器低于的方法
注意:它使用App\Http\Request\CreateUploadRequest;
public function store(CreateUploadRequest $request)
{
$file = Input::file('file');
$destinationPath = 'assets/uploads'; // upload path
$name = $file->getClientOriginalName(); // getting original name
$fileName = time().rand(11111, 99999) . '.' . $extension; // renaming image
$extension = $file->getClientOriginalExtension(); // getting fileextension
$file->save($destinationPath.'/'.$fileName); // uploading file to given path
}中间件
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
class CreateUploadRequest extends Request {
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'file' => 'required_if:update,false|mimes:pdf,doc,ppt,xls,docx,pptx,xlsx,rar,zip|max:1000'
];
}
}我想这个想法是从一段爆竹视频中得到的。我去看看能不能找到它。
https://stackoverflow.com/questions/33105910
复制相似问题