Zip File Upload in Php Problem

Maybe a mime type problem...

Page 1 of 1

5 Replies - 7705 Views - Last Post: 05 July 2009 - 11:40 AM Rate Topic: -----

#1 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Zip File Upload in Php Problem

Posted 05 July 2009 - 09:53 AM

Hello Everyone, I am writing a Php script in order to give the user the ability to upload .zip files to the server, I have tested the code with other file types such as .jpg and .gif images and they were uploaded successfully to the desired directory.

The problem I am having is that when I try to upload a .zip file the program seems not to be able to associate the demanded .zip file with the allowed mime type for the .zip files, and thus jumps directly jumps to the else statement that fires the message that says: "Sorry, you cannot upload this type of file!", so most probably I have mentioned the mime type there in a wrong way, can you please show me the right way to do this? I tried : "application/zip" , "archive/zip" , and "file/zip" were all of these didn't work. While other files as specified above did work.

Here is the code for the Php script that is responsible for the upload and saving of the file. it is called: upload.php and path is the name of the chosen file:

<?php
if($_FILES["path"]["type"] == "image/gif"
||  $_FILES_["path"]["type"] == "image/jpeg"
||  $$_FILES["path"]["type"] == "application/zip"  // problem in specifying the mime type here...
 && $_FILES["path"]["size"] < 100000) { // the limit is around 100 kb
 if($_FILES["path"]["error"] > 0)
   echo "An error has occured!".$_FILES["path"]["error"]."<br/ >";

else
	echo "Upload: " . $_FILES["path"]["name"] . "<br />";
	echo "Type: " . $_FILES["path"]["type"] . "<br />";
	echo "Size: " . ($_FILES["path"]["size"] / 1024) . " Kb<br />";
	echo "Stored in: " . $_FILES["path"]["tmp_name"];

	echo '<script language = "javascript">
		  alert("The file has been successfully uploaded!"); 
		  </script>';

  if (file_exists("upload/" . $_FILES["path"]["name"])) //checks if the file already exists in the directory
	  {
	  echo $_FILES["path"]["name"] . " already exists. ";
	  }
	else
	  {
	  move_uploaded_file($_FILES["path"]["tmp_name"], 
	  "upload/" . $_FILES["path"]["name"]);
	  echo "Stored in: " . "upload/" . $_FILES["path"]["name"];
	  }
}
else // in case the above criteria was not met
  echo '<script language = "javascript">alert("Sorry, you cannot upload this type of file!");</script>';
?>



Here is the file containing the form responsible for comunicating with the above script and for choosing the file, in case you need it:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>File Upload test</title>
		<style type="text/CSS">
		feildset{width : 20%; color:orange};
		</style>
	</head>
	<body>

	<center>
		<form method = "post" name = "upload" action = "upload.php" enctype="multipart/form-data">
		<feildset>
		<label id = "title" name = "title">Enter the path to the file you want to upload!</label>
		<br>
		<br>
		<label id="fileUpload">Upload:</label>
		<input type = "file" name = "path" id = "path" size = "30">
		<input type = "submit" name = "submit" value = "Upload!">
		</feildset>
		</form>
	  </center>
	</body>
</html>



Any help, comments, tips , ideas and solutions are welcome, Thanks in advance :)

This post has been edited by ayman_mastermind: 05 July 2009 - 09:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Zip File Upload in Php Problem

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Zip File Upload in Php Problem

Posted 05 July 2009 - 10:06 AM

I'm fairly sure it gets uploaded as application/x-compressed. I may be wrong, but you can always print_r($_FILES); and find out what type is being sent to the server, just do that and find out :)

EDIT, possible options:
application/zip
application/x-zip
application/x-zip-compressed
application/octet-stream
application/x-compress
application/x-compressed
multipart/x-zip

This post has been edited by MageUK: 05 July 2009 - 10:07 AM

Was This Post Helpful? 0
  • +
  • -

#3 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Zip File Upload in Php Problem

Posted 05 July 2009 - 11:22 AM

Quote

I'm fairly sure it gets uploaded as application/x-compressed. I may be wrong, but you can always print_r($_FILES); and find out what type is being sent to the server, just do that and find out smile.gif

EDIT, possible options:
application/zip
application/x-zip
application/x-zip-compressed
application/octet-stream
application/x-compress
application/x-compressed
multipart/x-zip


Hello, MageUK i used your tip and tried print_r($_FILES); to find out what type is being sent to the server, the type turned out to be: application/x-zip-compressed so what i did is replaced my old mime type with the new one, but the problem remained the same :/ what do you think? Do you think i should try to change the server or something? Thanks in advance and thanks for your previous tip :)

This post has been edited by ayman_mastermind: 05 July 2009 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Zip File Upload in Php Problem

Posted 05 July 2009 - 11:29 AM

Erm, did you copy this error when checking the type??

$$_FILES["path"]["type"] == "application/zip"

Notice the double $. From what I can see it should work fine apart from that.
Was This Post Helpful? 0
  • +
  • -

#5 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Zip File Upload in Php Problem

Posted 05 July 2009 - 11:38 AM

Quote

Erm, did you copy this error when checking the type??

$$_FILES["path"]["type"] == "application/zip"

Notice the double $. From what I can see it should work fine apart from that.


Actually yeah I realized and removed the second '$' when I was editing the mime type, still the problem is there :(

Oh never mind, i solved the problem, there was a typo in the new mime type I entered :P (I am really tired and didn't realize my mistake) now i have it corrected and it is working, thank you very much MageUK and thanks for the tip, have a good day :)

This post has been edited by ayman_mastermind: 05 July 2009 - 11:40 AM

Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Zip File Upload in Php Problem

Posted 05 July 2009 - 11:40 AM

Hehe no problem, glad it's all working :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1