1

Possible Duplicate:
uploaded file type check by PHP

I used the below script for checking file type while uploading to restrict file uploading to only jpg/jpeg,png.But it doesnt work with IE(works with mozilla).So i need a script which works with all browsers.

$destination_path = getcwd().DIRECTORY_SEPARATOR;

$target_path = $destination_path . basename( $_FILES['myfile']['name']);
$types=array('image/png','image/jpeg');

if (in_array($_FILES['myfile']['type'], $types))
{
if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {   
$result = 1;
}
}

else
{
$result = 0;
}
Community
  • 1
  • 1

2 Answers2

1

IE sets different file type header for jpg, so add image/pjpeg to your $types.

$types=array('image/png','image/jpeg', 'image/pjpeg');
Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70
1

a little bit time on google and you would have found it this works perfectly for all browsers

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
$_FILES["file"]["name"]=$_FILES["file"]["name"];
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 50000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);

  $filename= "upload/" . $_FILES["file"]["name"];
  }
}
 }
else
{
echo "Invalid file";
}