Uploading an Image in PHP Gives a Black Box (Another Internet Explorer Error)
Really, I’m tired of supporting IE. Half the time, I find a “mistake” in my HTML coding that I didn’t catch in Firefox, because Firefox is so forgiving with semantics. The other half of my IE errors are really IE’s fault. Let’s take this story (and fix) as one of the flagship examples of Microsoft Idiocracy.
I build a simple image upload script in PHP. On the page where the form posts its data to, I used PHP’s built in getimagesize function to get data about the image. What I wanted was the mime-type–for example if it was a JPG, GIF, or PNG–so I could do further processing on it.
For JPG images, the mime type is “img/jpeg”, for GIF–”img/gif”, and so on. Once you know the mime type, you can use PHP’s imagecreatefromjpeg, for example, to create a new image from the JPG file that was uploaded.
So, in my code, if I wanted to do a switch statement based on the image type, it might look something like this:
$image_data = getimagesize($image_file);
switch($image_data['mime']) {
case “image/jpeg”:
// Do code for a JPG image here
break;
case “image/gif”:
// Do code for a GIF image here
break;
}
In most cases, this works perfectly. That is, until you get to Internet Explorer. Internet Explorer interprets the mime type of a JPG image as “image/pjpeg”, breaking your code, breaking the standards, driving you batty, and adding to it’s lengthy list of inexplicable ways of doing things. To fix, you’d simply change the code to:
case "image/jpeg":
case "image/pjpeg":
// Do code for a JPG image here
break;
… and the world is right again.




Acrobatic
