PHP -文件上传安全,为什么这个方法不能工作


PHP - File Upload Security, Why Won't This Method Work?

我是第一次上传文件,我意识到安全是巨大的。现在,根据测试目的,在我真正想要深入研究安全性之前,我只想快速地进行测试上传,当我尝试以下"security"

<?PHP
$name = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
if(isset($name)) {
    if (!empty($name)) {
    $location = "ufiles/";  
    if($_FILES['file']['type'] == "images/jpeg" || $_FILES['file']['type'] == "images/png"){
    move_uploaded_file($tmp_name, $location.$name);
                }
            }
        }
?>

此代码似乎不起作用,除非我取消文件类型检查点,否则文件将无法上传。所以这显然是我的问题。但为什么这不起作用呢?

也:我意识到这不是一个非常有效的保护文件类型的方法,但它似乎最简单的逻辑,我只是很困惑为什么它不工作。

编辑总是检查语法。lol

if($_FILES['file']['type'] == "images/jpeg" || $_FILES['file']['type'] == "images/png")   

"图像/jpeg"不是"形象 s /jpeg"。对于PNG类型也是如此。

$_files也应该是$_files正如Oberst &大衮。PHP变量是区分大小写的。

一样:

if(isset($name)) {
    if (!empty($name)) {
    $location = "ufiles/";  
    if($_FILES['file']['type'] == "image/jpeg" || $_FILES['file']['type'] == "image/png"){
    move_uploaded_file($tmp_name, $location.$name);
                }
            }
        }
?>

我认为这是一个问题

$_files['file']['type'] == "images/jpeg" || $_files['file']['type'] == "images/png";

请替换$_FILES['file']['type'],或者您可以按照上面的定义替换$type

尝试将内容类型更改为image/jpegimage/png

这可以很容易地绕过,因为$_FILES['file']['type']中的媒体类型可以伪造。

如果您只想允许图像,请确保使用适当的文件扩展名保存它们。否则,攻击者可以将.php文件上传为image/png,这将绕过您的验证。