如何正确验证PHP中的图像?

时间:2016-04-08 16:19:07

标签: php validation file-upload

我没有得到我想要的东西,

这是我的代码:

if (isset($_FILES["avatar"]["name"]) && $_FILES["avatar"]["tmp_name"] != "") { /* Main IF statement starts Here */

    $fileName = $_FILES["avatar"]["name"];
    $fileTmpLoc = $_FILES["avatar"]["tmp_name"];
    $fileType = $_FILES["avatar"]["type"];
    $fileSize = $_FILES["avatar"]["size"];
    $fileErrorMsg = $_FILES["avatar"]["error"];
    $kaboom = explode(".", $fileName);

    $fileExt = end($kaboom);

    list ($width, $height) = getimagesize($fileTmpLoc);
    if ($width < 10 || $height < 10) {
        header("Location: a.php?err=2");
        exit();
    }

    $db_file_name = rand(100000000000, 999999999999) . "." . $fileExt;

    if ($fileSize > 1048576) {
        header("Location: a.php?err=3");
        exit();
    } 

    elseif (! preg_match("/\.(gif|jpg|png)$/i", $fileName)) {
        header("Location: a.php?err=4");
        exit();
    } 

    elseif ($fileErrorMsg == 1) {
        header("Location: a.php?err5");
        exit();
    }
}  /* Main IF statement ends Here */

else {
    header("Location: go_back.php?err=2");
    exit();
}

宽度和高度验证工作正常,但if ($fileSize > 1048576) filesize大于1MB,然后重定向到go_back.php?err=2,我希望它重定向到a.php?err=3

0 个答案:

没有答案