Page 1 of 1

mime types with php uploader works in mozilla etc and not IE

Posted: Tue Jan 13, 2004 4:11 pm
by robster
Surprise surprise! :)

Well, here is what happens:

In Mozilla and Safari and Firebird etc etc my image upload form works just fine.

In IE I get this error:

Code: Select all

Only image/jpeg files may be uploaded.
Looking at my code snippet, here is the part that has the mime type stuff:

Code: Select all

#--------------------------------#
# Variables
#--------------------------------#

// The path to the directory where you want the 
// uploaded files to be saved. This MUST end with a 
// trailing slash unless you use $path = ""; to 
// upload to the current directory. Whatever directory
// you choose, please chmod 777 that directory.

	$path = "d:/path/to/site/html/files/";

// The name of the file field in your form.

	$upload_file_name = "userfile";

// ACCEPT mode - if you only want to accept
// a certain type of file.
// possible file types that PHP recognizes includes:
//
// OPTIONS INCLUDE:
//  text/plain
//  image/gif
//  image/jpeg
//  image/png
	
	// Accept ONLY gifs's
	#$acceptable_file_types = "image/gifs";
	
	// Accept JPEG files
	$acceptable_file_types = "image/jpeg";
	
	
	// Accept ALL files
	#$acceptable_file_types = "";

// If no extension is supplied, and the browser or PHP
// can not figure out what type of file it is, you can
// add a default extension - like ".jpg" or ".txt"

	$default_extension = ".jpg";

// MODE: if your are attempting to upload
// a file with the same name as another file in the
// $path directory
//
// OPTIONS:
//   1 = overwrite mode
//   2 = create new with incremental extention
//   3 = do nothing if exists, highest protection

	$mode = 1;


	
#--------------------------------#
# PHP
#--------------------------------#
	if (isset($_REQUEST['submitted'])) {
	
	
			
		/* 
			A simpler way of handling the submitted upload form
			might look like this:
			
			$my_uploader = new uploader('en'); // errors in English
	
			$my_uploader->max_filesize(30000);
			$my_uploader->max_image_size(800, 800);
			$my_uploader->upload('userfile', 'image/gif', '.gif');
			$my_uploader->save_file('uploads/', 2);
			
			if ($my_uploader->error) {
				print($my_uploader->error . "<br><br>\n");
			} else {
				print("Thanks for uploading " . $my_uploader->file['name'] . "<br><br>\n");
			}
		*/
			
		// Create a new instance of the class
		$my_uploader = new uploader($_POST['language']); // for error messages in french, try: uploader('fr');
		
		// OPTIONAL: set the max filesize of uploadable files in bytes
		$my_uploader->max_filesize(1500000);
		
		// OPTIONAL: if you're uploading images, you can set the max pixel dimensions 
		$my_uploader->max_image_size(2000, 2000); // max_image_size($width, $height)
		
		// UPLOAD the file
		if ($my_uploader->upload($upload_file_name, $acceptable_file_types, $default_extension)) {
			$my_uploader->save_file($path, $mode);
		}
		
		if ($my_uploader->error) {
			echo $my_uploader->error . "<br><br>\n";
		
		} else {
			// Successful upload!
			print("<b>DEBUG INFO:</b> ". $my_uploader->file['name'] . " was successfully uploaded! <a href="" . $_SERVER['PHP_SELF'] . "">Click here to upload another</a><br>");
			$oldfilename = "$my_uploader->file['name']";
			
			// Print all the array details...
			//print_r($my_uploader->file);
			

			}

I'm wondering if there is some bug in IE that is causing it to bail where mozilla/firebird/safari etc are all fine with it?

Is it a php thing (passing variables) that IE can't handle?

Has anyone come across such a thing?

VERY strange.

I really appreciate anyone looking at this. Again, it's one of those things I've spent a couple of days on now and am just finding it hard to see any angles that could be wrong now. Some fresh eyes perhaps?

Thanks a ton!

Rob

Posted: Tue Jan 13, 2004 4:23 pm
by basdog22
i have something simmular to your problem when i zip my files on the fly and send them as attachment.

IE asks me if i want to dl down.php?op=45.php 8O 8O 8O 8O

I choose open. and then it downloads 45.zip 8O 8O 8O

Ohhh... your post here :wink: :wink: :wink:

well you can always flash your server... I did it when everything went wrong and BOOM everything worked OK :roll:

Posted: Tue Jan 13, 2004 5:36 pm
by robster
Unfortunately it's not my server, it's a shared windoze server :(
I can't just flush my server and reboot etc.

Does anyone have any suggestions as to what this might be and how I can get IE to accept a jpg with my code? I'm at my ends :(


ta :)

Rob

Posted: Wed Jan 14, 2004 4:07 am
by twigletmac
Print out $_FILES and see what IE thinks the mime type is.

Mac

Posted: Wed Jan 14, 2004 5:24 pm
by robster
I am not too clever with all this yet so please bear with me if I did this wrong but this is what I did:


echo "$_FILES";

Safari writes to the browser: Array

I have to try IE as soon as a mate with IE for win comes online or tomorrow at work (shhhhh ;)) but I wonder if you might let me know if what I've done there is correct or am I missing something extra in my echo command?

thanks again, this is really appreciated :)

Rob

Posted: Thu Jan 15, 2004 1:35 am
by robster
IE says the same thing.... "ARRAY"

Posted: Thu Jan 15, 2004 3:21 am
by twigletmac
To view the contents of an array you need to use [php_man]print_r[/php_man]():

Code: Select all

echo '<pre>';
print_r($_FILES);
echo '</pre>';
Apologies for not including the code snippet in my previous post.

Mac

Posted: Thu Jan 15, 2004 4:42 am
by robster
WOW, ok, that's got some results :)
Thanks so much.

OK, here's what comes back:

Mozilla

Code: Select all

Array
(
    &#1111;userfile] => Array
        (
            &#1111;name] => mocap.jpg
            &#1111;type] => image/jpeg
            &#1111;tmp_name] => d:\PHP\uploadtemp\php226E.tmp
            &#1111;error] => 0
            &#1111;size] => 59486
        )

)

IE

Code: Select all

Array
(
    &#1111;userfile] => Array
        (
            &#1111;name] => mocap.jpg
            &#1111;type] => image/pjpeg
            &#1111;tmp_name] => d:\PHP\uploadtemp\php2270.tmp
            &#1111;error] => 0
            &#1111;size] => 59486
        )

)

So IE thinks it's of type pjpeg and everything else thinks it's of type jpeg.
Is there a way to edit my code to make it accept both perhaps?

I currently use $acceptable_file_types = "image/jpeg";

And again, thanks so much, this is really heartening...

Rob

Posted: Thu Jan 15, 2004 4:48 am
by twigletmac
How does the upload() function test to see if the file is of an acceptable type?

Mac

Posted: Thu Jan 15, 2004 6:23 am
by robster
I am using a script written by someone else. Here is the include file. I hope its length isn't a problem. If so let me know and i can edit it down afterwards.

:)
[Admin Edit: just cut it down to the one function so I didn't have to scroll, no worries I was just being lazy :)]

Code: Select all

<?php
	/**
	 * bool upload (string filename[, string accept_type[, string extension]]);
	 * 
	 * Checks if the file is acceptable and uploads it to PHP's default upload diretory
	 * 
	 * @param filename		(string) form field name of uploaded file
	 * @param accept_type	(string) acceptable mime-types
	 * @param extension		(string) default filename extenstion
	 * 
	 */
	function upload($filename='', $accept_type='', $extention='') {
		
		$this->acceptable_file_types = trim($accept_type); // used by error messages
		
		if (!isset($_FILES) || !is_array($_FILES[$filename]) || !$_FILES[$filename]['name']) {
			$this->error = $this->get_error(0);
			$this->accepted  = FALSE;
			return FALSE;
		}
				
		// Copy PHP's global $_FILES array to a local array
		$this->file = $_FILES[$filename];
		$this->file['file'] = $filename;
		
		// Initialize empty array elements
		if (!isset($this->file['extention'])) $this->file['extention'] = "";
		if (!isset($this->file['type']))      $this->file['type']      = "";
		if (!isset($this->file['size']))      $this->file['size']      = "";
		if (!isset($this->file['width']))     $this->file['width']     = "";
		if (!isset($this->file['height']))    $this->file['height']    = "";
		if (!isset($this->file['tmp_name']))  $this->file['tmp_name']  = "";
		if (!isset($this->file['raw_name']))  $this->file['raw_name']  = "";
				
		// test max size
		if($this->max_filesize && ($this->file["size"] > $this->max_filesize)) {
			$this->error = $this->get_error(1);
			$this->accepted  = FALSE;
			return FALSE;
		}
		
		if(stristr($this->file["type"], "image")) {
			
			/* IMAGES */
			$image = getimagesize($this->file["tmp_name"]);
			$this->file["width"]  = $image[0];
			$this->file["height"] = $image[1];
			
			// test max image size
			if(($this->max_image_width || $this->max_image_height) && (($this->file["width"] > $this->max_image_width) || ($this->file["height"] > $this->max_image_height))) {
				$this->error = $this->get_error(2);
				$this->accepted  = FALSE;
				return FALSE;
			}
			// Image Type is returned from getimagesize() function
			switch($image[2]) {
				case 1:
					$this->file["extention"] = ".gif"; break;
				case 2:
					$this->file["extention"] = ".jpg"; break;
				case 3:
					$this->file["extention"] = ".png"; break;
				case 4:
					$this->file["extention"] = ".swf"; break;
				case 5:
					$this->file["extention"] = ".psd"; break;
				case 6:
					$this->file["extention"] = ".bmp"; break;
				case 7:
					$this->file["extention"] = ".tif"; break;
				case 8:
					$this->file["extention"] = ".tif"; break;
				default:
					$this->file["extention"] = $extention; break;
			}
		} elseif(!ereg("(\.)([a-z0-9]{3,5})$", $this->file["name"]) && !$extention) {
			// Try and autmatically figure out the file type
			// For more on mime-types: http://httpd.apache.org/docs/mod/mod_mime_magic.html
			switch($this->file["type"]) {
				case "text/plain":
					$this->file["extention"] = ".txt"; break;
				case "text/richtext":
					$this->file["extention"] = ".txt"; break;
				default:
					break;
			}
		} else {
			$this->file["extention"] = $extention;
		}
		
		// check to see if the file is of type specified
		if($this->acceptable_file_types) {
			if(trim($this->file["type"]) && stristr($this->acceptable_file_types, $this->file["type"])) {
				$this->accepted = TRUE;
			} else { 
				$this->accepted = FALSE;
				$this->error = $this->get_error(3);
			}
		} else { 
			$this->accepted = TRUE;
		}
		
		return (bool) $this->accepted;
	}?>

Posted: Thu Jan 15, 2004 6:39 am
by twigletmac
Hmm, not entirely sure if it'll work but try changing:

Code: Select all

$acceptable_file_types = "image/jpeg";
to

Code: Select all

$acceptable_file_types = "image/jpeg image/pjpeg";
Mac

Posted: Thu Jan 15, 2004 6:47 am
by robster
HOORAY!!

:)

It was NEARLY what you suggested. I made it so:

Code: Select all

$acceptable_file_types = "image/jpeg|image/pjpeg";
and just like that it works! I want to thank you and this forum again. Such a great resource and so helpful. :)

What I find interesting (anoying) with this problem was that IE was the ONLY browser that would not accept it and saw a jpg as a progressive jpg. All the rest saw the jpg as a jpg. Would that indicate that IE is seeing them as they are and the others are being lazy or would it mean the others are being accurate and IE is not reading the filetypes properly? (I tried SO many jpgs from all over the place with the same results.)

Either way it works now, so thanks again :) I hope this helps someone in the future browsing through the forums.

Rob

Posted: Thu Jan 15, 2004 6:56 am
by twigletmac
Glad you got it working :).

I'm not sure why IE does it differently - I would say (but them I'm biased) that Moz is doing it right and IE is wrong :lol:

Mac