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.
Archives
- May 2012
- April 2012
- March 2012
- January 2012
- December 2011
- November 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2010
- December 2009
- September 2009
- August 2009
- July 2009
- April 2009
- February 2009
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- May 2007
- April 2007
- March 2007
- January 2007
- December 2006
- October 2006
- August 2006



Posted under: