开发者

PHP File Upload Recognition help!

开发者 https://www.devze.com 2023-03-04 15:26 出处:网络
SOLVED MY OWN QUESTION! THANKS EVERYONE FOR THE HELP :) Ok. I\'m having trouble with the code below recognizing the upload FILE TYPE and running the correct function. I can upload PNG just fine and i

SOLVED MY OWN QUESTION! THANKS EVERYONE FOR THE HELP :)

Ok. I'm having trouble with the code below recognizing the upload FILE TYPE and running the correct function. I can upload PNG just fine and it will convert and resize like it should, but GIF and JPEG don't and just return a black image. If I remove the png code and try the others individually they work. I can't figure this out at the moment why when I combine them they won't work. It's like together they all use whatever function comes first, instead of going by the 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开发者_C百科/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;
    }
}


edit: combined with Pekka's method for the mime, and rewritten for clarity

You have an error in all your if/elseif comparations. You need to put double == instead of single =

You may use this code that should do the same, but in a cleaner and safer way

$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;

Also, don't rely on $_FILE['image']['type'], as this value is sent by the browser and an attacker can forge it. Use the getimagesize() method for obtaining the filetype as Pekka suggested in his answer.


@Carlos answers your question.

As a side note, I wouldn't rely on the MIME type server by the user's browser at all, and use getimagesize() to detect the file type instead.

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

that is safer.


FIGURED IT OUT!!!!!!!! The verification was screwing things up. Instead of checking if it's not a image, I checked if it was and it started working. NEW verification -> if ($mime == 'image/gif' || $mime == 'image/jpeg' || $mime == 'image/pjpeg' || $mime == 'image/png' || $_FILES['image']['size'] < 3000000){ working code here } else { error code here } . Thanks for all the help!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号