php file upload type restriction


#1

hi guys i got this codes for php
is supposed to allow all kind of images,text files(.txt) and zip but the zip and txt are not working
any hint ?
what am i doing wrong ?
thank in advance for any help

[php]
if ((($_FILES[“file”][“type”] == “image/gif”)
|| ($_FILES[“file”][“type”] == “image/jpeg”)
|| ($_FILES[“file”][“type”] == “application/zip”)
|| ($_FILES[“file”][“type”] == “image/png”)
|| ($_FILES[“file”][“type”] == “image/bmp”)
|| ($_FILES[“file”][“type”] == “text/txt”)
|| ($_FILES[“file”][“type”] == “image/pjpeg”))
&& ($_FILES[“file”][“size”] < 8388608))
[/php]

if you need the whole php file codes just let me know


#2

yep, you’re closing the if statement to early. I’d seperate out the size restriction though.


#3

Thank you for your repply i separated the size and type restriction :

here is the whole file code :

[php]
if (!empty($_FILES[“file”])) {
if (($_FILES[“file”][“type”] == “image/gif”)
|| ($_FILES[“file”][“type”] == “image/jpeg”)
|| ($_FILES[“file”][“type”] == “application/zip”)
|| ($_FILES[“file”][“type”] == “image/png”)
|| ($_FILES[“file”][“type”] == “image/bmp”)
|| ($_FILES[“file”][“type”] == “text documents/txt”)
|| ($_FILES[“file”][“type”] == “image/pjpeg”)){

if (!($_FILES[“file”][“size”] < 8388608)) {
echo "Return Code: " . $_FILES[“file”][“error”] . “
”;
}

if ($_FILES[“file”][“error”] > 0)
{
echo "Return Code: " . $_FILES[“file”][“error”] . “
”;
}
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"]);
 echo "The file " . $_FILES["file"]["name"] . " was susessfully uploaded";
  }
}

}
else
{
echo “Invalid file”;
}
}
[/php]

the size and images type works great but i also wanna be able to upload zip and txt


#4

The mime text is application/plain or text/plain and zip is application/x-compressed, application/x-zip-compressed, application/zip, or multipart/x-zip.

Here’s a full listing of the mime types - http://www.webmaster-toolkit.com/mime-types.shtml


#5

thanks for the mime sites really helpful.

the text worked but the zip only worked in internet explorer.
i tried it one by one using all the different zip mime.
I also tried all together like this:
[php]
|| ($_FILES[“file”][“type”] == “application/x-compressed”)
|| ($_FILES[“file”][“type”] == “application/x-zip-compressed”)
|| ($_FILES[“file”][“type”] == “application/zip”)
|| ($_FILES[“file”][“type”] == “multipart/x-zip”)
[/php]
but still didn’t work for google chrome and firefox.

could it the problem be somewhere else?


#6

yea, there seems to be a glitch for that one in chrome, but some suggested trying application/octet-stream for zip.


#7

Thankz will try that when i get home and let u know how it went