Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP upload file won't work in IE

Posted on 2009-04-29
11
Medium Priority
?
1,301 Views
Last Modified: 2013-12-08
Hi, I have a form that allows a user to upload a jpg (see Form Page code below) which sends the file to another page for processing (changes the size and saves it for use later)

Everything works fine except in IE, whenever I try to upload using IE I get the message "Invalid file. Please click here to try again" (see end of if statement at bottom of code).

Any ideas what I can do to fix this?

Please ignore the php gd code as I know this works
Form Page
<form action="upload.php" method="post" enctype="multipart/form-data" name="imgUploadForm" id="imgUploadForm">
               <p>Item name: 
                 <input type="text" name="itemName2" id="itemName2" />
               </p>
               <p>Item image: 
                 <input type="file" name="imgUpload" id="imgUpload" />
               </p>
               <p>
                 <input type="submit" name="uploadSubmit" id="uploadSubmit" value="Upload Image" />
               </p>
             </form>
 
Processing Page
<?php
if ((($_FILES["imgUpload"]["type"] == "image/jpg")
|| ($_FILES["imgUpload"]["type"] == "image/jpeg"))
&& ($_FILES["imgUpload"]["size"] < 2097152))
  {
  if ($_FILES["imgUpload"]["error"] > 0)
    {
    echo "There has been an error, your file is either too large of it is of the wrong type. Please make sure the image you are uploading ends in either .jpg or .jpeg and it is less than 2MB in size.<br /> Technical stuff - Return Code: " . $_FILES["imgUpload"]["error"] . "<br />";
    }
  else
    {
 
    if (file_exists("upload/" . $_FILES["imgUpload"]["name"]))
      {
      echo $_FILES["imgUpload"]["name"] . " already exists. ";
      }
    else
      {			
			switch($_FILES['imgUpload']['type'])
			{
				case "image/gif":
					$exts = "gif";
					break;
				case "image/jpg":
					$exts = "jpg";
					break;
				case "image/jpeg":
					$exts = "jpg";
					break;
				case "image/png":
					$exts = "png";
					break;
				case "image/pjpeg":
					$exts = "pjpeg";
					break;
				
			}
		
		$uploadedfile = $_FILES["imgUpload"]["tmp_name"];
		$src = imagecreatefromjpeg($uploadedfile);
		list($width,$height) = getimagesize($uploadedfile);
		$newwidth = 100;
		$newheight = ($height/$width)*$newwidth;
		$tmp = imagecreatetruecolor($newwidth,$newheight);
		
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
			
		$ran = rand () ;
		$ran2 = $ran.".";
		$newfile = $ran2.$exts;
		$target = "upload/";
		$target = $target.$ran2;
		$target = $target.$exts; 
		if(imagejpeg($tmp,$target,75))
		{
			echo "The file has been uploaded.";
		}
		else
		{
		echo "Sorry, there was a problem uploading your file. Please use your browser's BACK button to try again. ".$target;
		}
      }
    }
  }
else
  {
  echo "<p>Invalid file. <a href='additem.php'>Please click here</a> to try again.</p>";
  }
  
  imagedestroy($src);
  imagedestroy($tmp);
?>

Open in new window

0
Comment
Question by:Romolo
  • 4
  • 4
  • 3
11 Comments
 
LVL 6

Expert Comment

by:basic612
ID: 24259794
Can you try this modified version of the script. It will show you the structure of the files array in  case of error. That might give you a clue as to why the error is happening.

If you are still stumped after seeing this, please post back the new output after:

Debug $_FILES

Form Page
<form action="upload.php" method="post" enctype="multipart/form-data" name="imgUploadForm" id="imgUploadForm">
               <p>Item name: 
                 <input type="text" name="itemName2" id="itemName2" />
               </p>
               <p>Item image: 
                 <input type="file" name="imgUpload" id="imgUpload" />
               </p>
               <p>
                 <input type="submit" name="uploadSubmit" id="uploadSubmit" value="Upload Image" />
               </p>
             </form>
 
Processing Page
<?php
if ((($_FILES["imgUpload"]["type"] == "image/jpg")
|| ($_FILES["imgUpload"]["type"] == "image/jpeg"))
&& ($_FILES["imgUpload"]["size"] < 2097152))
  {
  if ($_FILES["imgUpload"]["error"] > 0)
    {
    echo "There has been an error, your file is either too large of it is of the wrong type. Please make sure the image you are uploading ends in wither .jpg or .jpeg and it is less than 2MB in size.<br /> Technical stuff - Return Code: " . $_FILES["imgUpload"]["error"] . "<br />";
    }
  else
    {
 
    if (file_exists("upload/" . $_FILES["imgUpload"]["name"]))
      {
      echo $_FILES["imgUpload"]["name"] . " already exists. ";
      }
    else
      {                 
                        switch($_FILES['imgUpload']['type'])
                        {
                                case "image/gif":
                                        $exts = "gif";
                                        break;
                                case "image/jpg":
                                        $exts = "jpg";
                                        break;
                                case "image/jpeg":
                                        $exts = "jpg";
                                        break;
                                case "image/png":
                                        $exts = "png";
                                        break;
                                case "image/pjpeg":
                                        $exts = "pjpeg";
                                        break;
                                
                        }
                
                $uploadedfile = $_FILES["imgUpload"]["tmp_name"];
                $src = imagecreatefromjpeg($uploadedfile);
                list($width,$height) = getimagesize($uploadedfile);
                $newwidth = 100;
                $newheight = ($height/$width)*$newwidth;
                $tmp = imagecreatetruecolor($newwidth,$newheight);
                
                imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
                        
                $ran = rand () ;
                $ran2 = $ran.".";
                $newfile = $ran2.$exts;
                $target = "upload/";
                $target = $target.$ran2;
                $target = $target.$exts; 
                //echo "extension = ".$exts."<br />";
                if(imagejpeg($tmp,$target,75))
                {
                        echo "The file has been uploaded.";
                }
                else
                {
                echo "Sorry, there was a problem uploading your file. Please use your browser's BACK button to try again. ".$target;
                }
                
                /*
                if(move_uploaded_file($_FILES['imgUpload']['tmp_name'], $target))
                {
                echo "The file has been uploaded.";
                
                }
                else
                {
                echo "Sorry, there was a problem uploading your file.";
                }
*/
      }
    }
  }
else
  {
  	// Debug $_FILES
  	echo '<hr noshade /><strong>Debug $_FILES</strong><pre>'.print_r($_FILES,true)."</pre><hr noshade />\n";
  	
  echo "<p>Invalid file. <a href='additem.php'>Please click here</a> to try again.</p>";
  }
  
  imagedestroy($src);
  imagedestroy($tmp);
?>

Open in new window

0
 
LVL 7

Author Comment

by:Romolo
ID: 24259902
the page comes back with this:

Debug $_FILESArray
(
    [imgUpload] => Array
        (
            [name] => image-file.jpg
            [type] => image/pjpeg
            [tmp_name] => C:\WINDOWS\TEMP\php784.tmp
            [error] => 0
            [size] => 49894
        )

)

It seems to think that the jpeg is a pjpeg ([type] => image/pjpeg) which I have decided not to allow in the upload.

0
 
LVL 5

Expert Comment

by:cdaugustin
ID: 24259939
hi,

IE send image/pjpeg (microsoft genius at work) ... so you need to add that in your IF
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Author Comment

by:Romolo
ID: 24259951
cdaugustin: sorry I don't understand what you mean
0
 
LVL 6

Expert Comment

by:basic612
ID: 24259964
image/pjpeg would indicate that the file is saved as a progressive jpeg.

can you test with another jpeg image that is not saved as progressive.

alternatively allow pjpeg as a vaild image type - I would think that GD should handle it fine. Can you confirm?

Form Page
<form action="upload.php" method="post" enctype="multipart/form-data" name="imgUploadForm" id="imgUploadForm">
               <p>Item name: 
                 <input type="text" name="itemName2" id="itemName2" />
               </p>
               <p>Item image: 
                 <input type="file" name="imgUpload" id="imgUpload" />
               </p>
               <p>
                 <input type="submit" name="uploadSubmit" id="uploadSubmit" value="Upload Image" />
               </p>
             </form>
 
Processing Page
<?php
if ((($_FILES["imgUpload"]["type"] == "image/jpg")
|| ($_FILES["imgUpload"]["type"] == "image/jpeg")
|| ($_FILES["imgUpload"]["type"] == "image/pjpeg"))
&& ($_FILES["imgUpload"]["size"] < 2097152))
  {
  if ($_FILES["imgUpload"]["error"] > 0)
    {
    echo "There has been an error, your file is either too large of it is of the wrong type. Please make sure the image you are uploading ends in wither .jpg or .jpeg and it is less than 2MB in size.<br /> Technical stuff - Return Code: " . $_FILES["imgUpload"]["error"] . "<br />";
    }
  else
    {
 
    if (file_exists("upload/" . $_FILES["imgUpload"]["name"]))
      {
      echo $_FILES["imgUpload"]["name"] . " already exists. ";
      }
    else
      {                 
                        switch($_FILES['imgUpload']['type'])
                        {
                                case "image/gif":
                                        $exts = "gif";
                                        break;
                                case "image/jpg":
                                        $exts = "jpg";
                                        break;
                                case "image/jpeg":
                                        $exts = "jpg";
                                        break;
                                case "image/png":
                                        $exts = "png";
                                        break;
                                case "image/pjpeg":
                                        $exts = "jpg";
                                        break;
                                
                        }
                
                $uploadedfile = $_FILES["imgUpload"]["tmp_name"];
                $src = imagecreatefromjpeg($uploadedfile);
                list($width,$height) = getimagesize($uploadedfile);
                $newwidth = 100;
                $newheight = ($height/$width)*$newwidth;
                $tmp = imagecreatetruecolor($newwidth,$newheight);
                
                imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
                        
                $ran = rand () ;
                $ran2 = $ran.".";
                $newfile = $ran2.$exts;
                $target = "upload/";
                $target = $target.$ran2;
                $target = $target.$exts; 
                //echo "extension = ".$exts."<br />";
                if(imagejpeg($tmp,$target,75))
                {
                        echo "The file has been uploaded.";
                }
                else
                {
                echo "Sorry, there was a problem uploading your file. Please use your browser's BACK button to try again. ".$target;
                }
                
                /*
                if(move_uploaded_file($_FILES['imgUpload']['tmp_name'], $target))
                {
                echo "The file has been uploaded.";
                
                }
                else
                {
                echo "Sorry, there was a problem uploading your file.";
                }
*/
      }
    }
  }
else
  {
        // Debug $_FILES
        echo '<hr noshade /><strong>Debug $_FILES</strong><pre>'.print_r($_FILES,true)."</pre><hr noshade />\n";
        
  echo "<p>Invalid file. <a href='additem.php'>Please click here</a> to try again.</p>";
  }
  
  imagedestroy($src);
  imagedestroy($tmp);
?>

Open in new window

0
 
LVL 5

Expert Comment

by:cdaugustin
ID: 24259978
when you upload an image file with IE 6, the browser sends the "image/pjpeg" for the MIME type (while the rest of the world does it "image/jpeg" or "image/jpg" ) so you need to add the following thing to your IF

if ((($_FILES["imgUpload"]["type"] == "image/jpg")
|| ($_FILES["imgUpload"]["type"] == "image/jpeg")
|| ($_FILES["imgUpload"]["type"] == "image/pjpeg")
)
&& ($_FILES["imgUpload"]["size"] < 2097152))  
0
 
LVL 7

Author Comment

by:Romolo
ID: 24260008
ah, of course, thanks
however I don't want pjpegs and for some reason if a file is saved as .pjpeg it will not display in the browser.
So FireFox, Chrome and Safari all handle jpg and jpeg even if the format is pjpeg. If I allow pjpeg then files ending in .pjpeg can get uploaded but the browsers will nto display them, that's why I want to disallow them.
How can I disallow them and allow IE to upload jpg's?
0
 
LVL 6

Expert Comment

by:basic612
ID: 24260013
a bit more optimisation:

Form Page
<form action="upload.php" method="post" enctype="multipart/form-data" name="imgUploadForm" id="imgUploadForm">
               <p>Item name: 
                 <input type="text" name="itemName2" id="itemName2" />
               </p>
               <p>Item image: 
                 <input type="file" name="imgUpload" id="imgUpload" />
               </p>
               <p>
                 <input type="submit" name="uploadSubmit" id="uploadSubmit" value="Upload Image" />
               </p>
             </form>
 
Processing Page
<?php
if (in_array($_FILES["imgUpload"]["type"],array("image/jpg","image/jpeg","image/pjpeg")) && $_FILES["imgUpload"]["size"] < 2097152)
  {
  if ($_FILES["imgUpload"]["error"] > 0)
    {
    echo "There has been an error, your file is either too large of it is of the wrong type. Please make sure the image you are uploading ends in wither .jpg or .jpeg and it is less than 2MB in size.<br /> Technical stuff - Return Code: " . $_FILES["imgUpload"]["error"] . "<br />";
    }
  else
    {
 
    if (file_exists("upload/" . $_FILES["imgUpload"]["name"]))
      {
      echo $_FILES["imgUpload"]["name"] . " already exists. ";
      }
    else
      {                 
                        switch($_FILES['imgUpload']['type'])
                        {
                                case "image/gif":
                                        $exts = "gif";
                                        break;
                                case "image/jpg":
                                case "image/jpeg":
                                case "image/pjpeg":
                                	$exts = "jpg";
                                        break;
                                case "image/png":
                                        $exts = "png";
                                        break;
                        }
                
                $uploadedfile = $_FILES["imgUpload"]["tmp_name"];
                $src = imagecreatefromjpeg($uploadedfile);
                list($width,$height) = getimagesize($uploadedfile);
                $newwidth = 100;
                $newheight = ($height/$width)*$newwidth;
                $tmp = imagecreatetruecolor($newwidth,$newheight);
                
                imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
                        
                $ran = rand () ;
                $ran2 = $ran.".";
                $newfile = $ran2.$exts;
                $target = "upload/";
                $target = $target.$ran2;
                $target = $target.$exts; 
                //echo "extension = ".$exts."<br />";
                if(imagejpeg($tmp,$target,75))
                {
                        echo "The file has been uploaded.";
                }
                else
                {
                echo "Sorry, there was a problem uploading your file. Please use your browser's BACK button to try again. ".$target;
                }
                
                /*
                if(move_uploaded_file($_FILES['imgUpload']['tmp_name'], $target))
                {
                echo "The file has been uploaded.";
                
                }
                else
                {
                echo "Sorry, there was a problem uploading your file.";
                }
*/
      }
    }
  }
else
  {
        // Debug $_FILES
        echo '<hr noshade /><strong>Debug $_FILES</strong><pre>'.print_r($_FILES,true)."</pre><hr noshade />\n";
        
  echo "<p>Invalid file. <a href='additem.php'>Please click here</a> to try again.</p>";
  }
  
  imagedestroy($src);
  imagedestroy($tmp);
?>

Open in new window

0
 
LVL 6

Accepted Solution

by:
basic612 earned 2000 total points
ID: 24260041
Check the code I have added above. Don't use the .pjpeg extension, jsut use .jpg and all will be fine.
0
 
LVL 5

Expert Comment

by:cdaugustin
ID: 24260062
hi,

IE is only sending the different MIME type, all you need to do is to take it in consideration, and put the jpg or jpeg extension for it (check attach) (also sugested by basic612 )
switch($_FILES['imgUpload']['type'])
{
        case "image/gif":
                $exts = "gif";
                break;
        case "image/jpg":
        case "image/jpeg":
        case "image/pjpeg":
        	$exts = "jpg";
                break;
        case "image/png":
                $exts = "png";
                break;
}

Open in new window

0
 
LVL 7

Author Closing Comment

by:Romolo
ID: 31575902
excellent, that looks like it's working, thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question