tags:

views:

438

answers:

3

I'm stumped. I've created an upload image process that works in every browser except Internet Explorer. I didn't check IE7 but IE8 seems to not check the file extension. I keep getting the error "you must upload a jpg, gif, bmp."

  /* image uploading */
  $target_path = "img/";
  $image = $_FILES['crebusimage'];
  $image['name'] = mysql_real_escape_string($image['name']);
  $target_path .= $crebustime."_".$image['name'];
  $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/jpe", "image/jfif", "image/png");

  $field = 'crebusimage';
  if(strlen($image['name']) == 0){
   $form->setError($field, "*please choose an image");
  }elseif(!in_array($image['type'], $valid_types)){
   $form->setError($field, "*You must upload a jpg, gif, or bmp");
  }else{
   $busimg = $crebustime."_".$image['name'];
   move_uploaded_file($image['tmp_name'], $target_path);
  }

 <td><b>Business Logo<br />(100Kb or less 100x100px)</b></td>
 <td><input type="file" name="crebusimage" value="1"></td>
 <td><input type="hidden" name="MAX_FILE_SIZE" value="100000" /></td>
 <td><?php echo $form->error('crebusimage'); ?></tD>

How can I make this thing work in evil IE...

+1  A: 

You should be checking the file extensions, not the headers sent along with the image. Maybe IE doesn't send these as they can be faked.

Juddling
How would I check the file extension, I thought that was what I was doing with image['type'].
ivannovak
$filename = basename($_FILES['uploadedfile']['name']); $ext = substr($filename, strrpos($filename, '.') + 1); if($ext == "jpg" || $ext == "png" || $ext == "gif") { // then check your extention }
Juddling
no. You would be suprised how many png/gif's are saved as JPG and visa versa. You really want to look at the header.
Byron Whitlock
Glad to know I'm doing something right : )
ivannovak
What about if i upload image.php and spoof the header as image/gif?
Juddling
+1  A: 

See what you're getting in your $_FILES array by executing this:

echo '<pre>';
print_r($_FILES);
echo '</pre>';

and use getimagesize to determine the image type, as mime types can be spoofed as @Juddling has correctly pointed out.

karim79
I'm getting: Array ( [name] => image.jpeg [type] => image/pjpeg [tmp_name] => /var/tmp/php78z4j9 [error] => 0 [size] => 3026)What's with the pjpeg? Rather than jpeg...
ivannovak
@ivannovak - I can see the problem now, there is no pjpeg mime type in your $valid_types array. Add "image/pjpeg" and it will work!
karim79
+2  A: 

If you're uploading jpegs, they generally get sent with the mimetype image/pjpeg in IE.

Specifically, I've encountered this problem with csv files. Windows machines will give you varying mimetypes depending on what you have installed to open csv files. /endgripe ;)

Akoi Meexx
So in IE, just for the sake of being difficult, adds a p to the beginning of file types? Thanks MS...
ivannovak
Tell me about it. I've not trusted mimetypes for a while now. Alternatively, one could just look for an instance of jpeg, png, bmp, or gif in the mimetype string and that would work as look as it's != -1
Akoi Meexx