PHP文件上传识别帮助!

时间:2011-05-10 07:10:20

标签: php image resize file-type

解决了我自己的问题!感谢每个人的帮助:))

确定。我在使用下面的代码时遇到问题,识别上传FILE TYPE并运行正确的功能。我可以很好地上传PNG,它会像它应该转换和调整大小,但GIF和JPEG不会,只返回一个黑色图像。如果我删除了png代码并单独尝试其他代码,则可以使用它们。我现在无法弄清楚为什么当我将它们组合起来时它们将无法工作。它就像在一起,他们都使用任何函数,而不是FILE TYPE

if ($width > $max_width){
    $scale = $max_width/$width;
    if ($_FILE['image']['type'] = "image/png"){
    $uploaded = resizeImagePNG($large_image_location,$width,$height,$scale);
    } elseif ($_FILE['image']['type'] = "image/gif"){
    $uploaded = resizeImageGIF($large_image_location,$width,$height,$scale);
    } elseif ($_FILE['image']['type'] = "image/jpeg" || $_FILE['image']['type'] = "image/pjpeg"){
    $uploaded = resizeImageJPG($large_image_location,$width,$height,$scale);
    }
    session_start();
    $_SESSION['image2resize'] = $large_image_location;
    }else{
    $scale = 1;
    if ($_FILE['image']['type'] = "image/png"){
    $uploaded = resizeImagePNG($large_image_location,$width,$height,$scale);
    } elseif ($_FILE['image']['type'] = "image/gif"){
    $uploaded = resizeImageGIF($large_image_location,$width,$height,$scale);
    } elseif ($_FILE['image']['type'] = "image/jpeg" || $_FILE['image']['type'] = "image/pjpeg"){
    $uploaded = resizeImageJPG($large_image_location,$width,$height,$scale);
    }
    session_start();
    $_SESSION['image2resize'] = $large_image_location;
    }
}

3 个答案:

答案 0 :(得分:3)

编辑:结合Pekka的mime方法,并为了清晰而重写

您的所有if / elseif比赛都有错误。你需要把double ==而不是single =

您可以使用此代码,但应以更清洁,更安全的方式执行此操作

$info = getimagesize(($_FILE['image']['tmp_name']); 
$mime = $info["mime"];

if ($width > $max_width){
    $scale = $max_width/$width;
} else {
    $scale = 1;
}

switch ($mime)
{
    case "image/png":
        $uploaded = resizeImagePNG($large_image_location,$width,$height,$scale);
        break;

    case "image/gif":
        $uploaded = resizeImageGIF($large_image_location,$width,$height,$scale);
        break;

    case "image/jpeg":
        $uploaded = resizeImageJPG($large_image_location,$width,$height,$scale);
        break;

    default:
        // do a better handling of the error
        die('image type not supported');
}

session_start();
$_SESSION['image2resize'] = $large_image_location;

此外,不要依赖$_FILE['image']['type'],因为此值是由浏览器发送的,攻击者可以伪造它。使用getimagesize()方法获取文件类型,如Pekka在其答案中所建议的那样。

答案 1 :(得分:2)

@Carlos回答你的问题。

作为旁注,我根本不会依赖用户浏览器的MIME类型服务器,而是使用getimagesize()来检测文件类型。

$info = getimagesize(($_FILE['image']['tmp_name']); 
$mime = $info["mime"];

更安全。

答案 2 :(得分:0)

想象出来!!!!!!!!验证搞砸了。而不是检查它是不是图像,我检查它是否是,它开始工作。新验证 - > if($ mime =='image / gif'|| $ mime =='image / jpeg'|| $ mime =='image / pjpeg'|| $ mime =='image / png'|| $ _FILES [' image'] ['size']< 3000000){{code code here} else {error code here}。谢谢你的帮助!