Image uploader, capital letters in JPG extension doesn't work

给你一囗甜甜゛ 提交于 2020-01-05 08:01:31

问题


I'm making an image uploader but for some reason i'm not allowed to upload JPG images in capital letters. How is this possible?

I also tried to add JPG to the allowedExts array but that is also not working.

$filesize = '8500'; // PUT the filesize here in KB

if(isset($_FILES["file"])){
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
var_dump($_FILES['file']['type']);
var_dump($extension);
if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/png")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < $filesize)
    && in_array($extension, $allowedExts)){
    if ($_FILES["file"]["error"] > 0){
        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
    else{       
        if (file_exists("source/images/" . $_FILES["file"]["name"])){
            echo 'image already exists';
        }
        else{
            //Upload original file to folder    
        }
    }
}
else{
    echo 'Wrong fileformat';
}

As output I get this:

string '' (length=0) string 'JPG' (length=3)

Wrong fileformat


回答1:


PHP string comparisons are case sensitive:

&& in_array($extension, $allowedExts)){

is going to blow up if you upload kitten.JPG, because .JPG is NOT in your allowed extensions array. .jpg is, but that's a completely different string as far as PHP is concerned. You should normalize the extension you get from the uploaded filename with strtolower, or at least use a case-insentive comparison, such as strcasecmp

And note that your file handling logic is incorrect. You've obviously grabbed a very widely distributed BAD example. The VERY first thing you need to check upon upload is the ['error'] parameter. If that's nonzero, then you cannot trust anything else in the $_FILES array for that particular file. Don't check size, don't check mime types, don't check filenames. If an upload fails, those could all be non-existent/incorrect/etc...




回答2:


$extension = end(explode(".", $_FILES["file"]["name"])); I think this is causing error. change it to, $extension = strtolower(end(explode(".", $_FILES["file"]["name"])));//change all into lowercase and then test.




回答3:


Where is $filesize defined? If you added JPEG in caps, should have worked.




回答4:


For other people that end up here: It's possible that the pictures with capital letters are from a different source (a camera with different settings) than the ones with lower case letters.

The ones with capital letters may be too large file size for example and you are not properly checking the $_FILES['file']['error'] property.

Check if you have an error in $_FILES['file']['error'] and compare to the list of errors: http://php.net/manual/en/features.file-upload.errors.php



来源:https://stackoverflow.com/questions/16252903/image-uploader-capital-letters-in-jpg-extension-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!