Page 1 of 1

file upload failing in internet explorer[solved]

Posted: Sat Aug 30, 2008 9:05 am
by swiftouch
http://verangomedia.com/~epn_belladsi/simplefileupload.html

This little script works in every browser(tested in FF,Opera,Safari) except Internet Explorer. I'm stumped as to why the image is failing on the copy from temp to "upload/" folder. Folder has 777 permissions and is chown'd correctly. Is there anything about IE that could cause this to fail only in IE? In IE it's failing on this line:

Code: Select all

if(move_uploaded_file($tmp_name, $target_path)) {

Code: Select all

 
session_start();
 
if (isset($_GET['grd'])) {
    if (isset($_POST['submit'])) {
        
        switch ($_GET['grd']) {
            case "upload":
            
            if (($_POST['cust_name'] == "") && (strlen($_POST['cust_name']) < 2)) {
                $_SESSION['status_msg'] = "Please fill in name(at least 2 characters)";
                break;
            }
 
            $image_types = array('image/bmp','image/jpeg','image/jpg','image/png','image/gif');
 
            $random_digit = rand(100000,999999);
 
            foreach ($_FILES["uploadedfile"]["error"] as $key => $error) {
                if ($error == UPLOAD_ERR_OK) {
                    
                    $target_path = "upload/$random_digit"."_{$_POST['cust_name']}_".basename($_FILES['uploadedfile']['name'][$key]); #folder needs to be chmoded to 777!!!
                    
                    if ($_FILES['uploadedfile']['size'][$key] > 5120000) {
                        $_SESSION['status_msg'] = "The file".$_FILES['uploadedfile']['name'][$key]." is too large. ";
                        break 1;
                    }
                    $tmp_name = $_FILES["uploadedfile"]["tmp_name"][$key];
 
                    if(!in_array(strtolower($_FILES["uploadedfile"]["type"][$key]), $image_types)) {
                        $_SESSION['status_msg'] .= $_FILES['uploadedfile']['name'][$key]." unknown file type...upload unsuccessful!<br />";
                        break 1;
                    }
 
                    if(move_uploaded_file($tmp_name, $target_path)) {
                        $_SESSION['status_msg'] .= $_FILES['uploadedfile']['name'][$key]."  <span style=\"color: green\">successfull!</span><br />";
                    } else {
                        $_SESSION['status_msg'] .= $_FILES['uploadedfile']['name'][$key]."  <span style=\"color: red\">upload unsuccessfull!</span><br />";
                    }
 
                }
            } # end foreach
            
            default:
                break;
        } # end switch
 
    } # end post submit
} # end get action

Code: Select all

<body>
<?
    if (isset($_SESSION['status_msg'])) {
        echo("<span style=\"color: red\">".$_SESSION['status_msg']."</span><br>");
        unset($_SESSION['status_msg']);
    }
?>
* = required<br /><br />
<form name="a" enctype="multipart/form-data" action="?grd=upload" method="post">
Name *<input type="text" name="cust_name" value="<?=$_POST['cust_name']?>" /><br /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
Choose file(s) to upload:<br /> 
<input name="uploadedfile[]" type="file" size="70" /> max file size: 5 meg<br />
<input name="uploadedfile[]" type="file" size="70" /><br />
<input name="uploadedfile[]" type="file" size="70" /><br />
<input name="uploadedfile[]" type="file" size="70" /><br />
<input type="submit" name="submit" value="Upload File" />
</form>
</body>

Re: file upload failing in internet explorer

Posted: Sat Aug 30, 2008 12:33 pm
by greyhoundcode
I had exactly this problem not long ago. A bit of trial and error revealed that the MIME types IE sends to the server (at least the version of IE I was using at the time) are non standard.

Memory fails me as to the exact difference but basically if I was expecting a type along the lines of image/jpeg then I was actually receiving image/x-jpeg or something like that, and so the file was failing the validation I had built into my own code.

I'm sorry I can't recall the actual type being sent but I am sure you will be able to determine this quite quickly by running a test using $_FILES['type'].

Re: file upload failing in internet explorer[solved]

Posted: Sat Aug 30, 2008 1:09 pm
by swiftouch
Thanks creators of IE for mucking things up again!!!

I just needed to test against this file type.

'image/pjpeg'

That was the fix. :banghead:

from w3schools:
Note: For IE to recognize jpg files the type must be pjpeg, for FireFox it must be jpeg.
If i had checked with a .gif file I would have realized where the problem was.