简体   繁体   English

奇怪的图像PHP验证

[英]strange image php validation

i have this script for an image validation. 我有这个脚本用于图像验证。 works fine with images with 500 kb or 1.5 mb for example, but if i try with images with 4mb or 6 for example, give the error Invalid format and not the "size large". 例如,对于500 kb或1.5 mb的图像,可以正常工作,但是如果我尝试使用4mb或6的图像,则给出错误格式无效而不是“尺寸大”。 Why? 为什么?

$imageData = @getimagesize($_FILES["userfile"]["tmp_name"]);

        if($imageData === FALSE || !($imageData[2] == IMAGETYPE_GIF || $imageData[2] == IMAGETYPE_JPEG || $imageData[2] == IMAGETYPE_PNG)) {
            echo "<li>Invalid format</li>";
            die();
        }
        else {
            if($_FILES["userfile"]["size"] >= 2000000) {
                echo "<li>The size large</li>";
                die();
            }
                else {
           //mystuff
}

thanks 谢谢

You're not checking if the file upload succeeded. 您没有检查文件上传是否成功。 PHP has a memory limit which applies to uploaded files, and your 'big' images are most likely over that limit. PHP有一个内存限制,适用于上传的文件,你的“大”图像很可能超过这个限制。 You should check the uploads like this: 您应该检查上传内容,如下所示:

if ($_FILES['userfile']['error'] === UPLOAD_ERR_OK) {
   ... uploaded ok ...
} else
   echo "File upload failed on 'userfile' with error: " . $_FILES['userfile']['error'];
}

You might also want to check your post_max_size and upload_max_filesize in php.ini. 您可能还想在php.ini中检查post_max_size和upload_max_filesize。 If the files you are trying to upload are too large, then I believe the _FILES array will not be populated. 如果您尝试上传的文件太大,我相信不会填充_FILES数组。

使用$ _FILES ['size'] ..对于大型图像,getimagesize非常慢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM