这是上传文件的好过滤器吗?

Hat*_*Hat 5 php security

我有一个网站,用户可以将文件上传到子目录.我正在过滤上传检查潜在的恶意代码.我是安全方面的新手,所以这看起来像是保护上传到服务器的最佳做法吗?如果没有,或者我错过了什么,你能指出我正确的方向吗?

//arrays with acceptable file extensions/types -- default validations set to false
$acceptable_ext = array('jpg', 'JPG', 'jpeg', 'JPEG', 'gif', 'GIF', 'png', 'PNG');
$acceptable_type = array('image/jpeg', 'image/gif', 'image/png');
$validated_ext = 0;
$validated_type = 0;

//validate file extension and type
if($_FILES && $_FILES['file']['name']) {

    $file_info = pathinfo($_FILES['file']['name']);

    //validate extension
    for ($x=0; $x < count($acceptable_ext); $x++) { 
        if($file_info['extension'] == $acceptable_ext[$x]) {
            $validated_ext = 1;
        }
    }
    //validate type
    for ($x=0; $x < count($acceptable_type); $x++) { 
        if($file_info['type'] == $acceptable_type[$x]) {
            $validated_type = 1;
        }
    }
}

if($validated_ext && $validated_type) {
   //upload file to the server blah blah
}
Run Code Online (Sandbox Code Playgroud)

Vis*_*nku 1

您可以在此处查看一些安全配置。

检查 MIME 类型、ini 配置、.htaccess 等将为您提供额外的安全性或额外的验证(根据链接)。