Jump to content

Problem with upload zip and then unzip


herghost

Recommended Posts

I have a wierd issue that I cant quite pin down.

 

Here is my code

if(isset($_GET['world']) && ($_GET['world'] == "upload"))

{
$foldername = $_POST['worldname'];
$target = "worlduploads/"; 
$ok=1;
$allowed_types = array("application/octet-stream","application/zip","application/x-zip");
$allowed_extensions = array("zip");

if ($_FILES['file']['size'] > 262144000) 
{
	$max_size =  round(25600);
	echo "Your file is too large. Maximum file size is 250MB. <br>"; 
	$ok=0;
} 

if ($_FILES["file"]["error"] > 0) 
{
	echo "Error: " . $_FILES["file"]["error"] . "<br />";
	$ok=0;
} 

else 
{
  		$path_parts = pathinfo(strtolower($_FILES["file"]["name"]));
  		
	if(in_array($_FILES["file"]["type"],$allowed_types) && in_array($path_parts["extension"],$allowed_extensions))
		{
			$filename = $_FILES["file"]["name"]; 
		}
	else 
		{
  				echo "Type " . $_FILES["file"]["type"] . "  with extension " . $path_parts["extension"] . " not allowed <br />";
				$ok=0;
			 }
}


if($ok == 1)
{
	move_uploaded_file($_FILES["file"]["tmp_name"], $target . $filename);
	$file_location = $target . $filename;

	if(file_exists($file_location))
		{
			mkdir('worlduploads/'.$foldername.''); //create local


			$dir = 'worlduploads/'.$foldername.'/';
    			$zip = zip_open($file_location);
    			while($zip_entry = zip_read($zip)) 
				{
					$entry = zip_entry_open($zip,$zip_entry);
					$filename = zip_entry_name($zip_entry);
					$target_dir = $dir.substr($filename,0,strrpos($filename,'/'));
					$filesize = zip_entry_filesize($zip_entry);
        				
						if (is_dir($target_dir) || mkdir($target_dir)) 
							{
            						if ($filesize > 0) 
									{
										$contents = zip_entry_read($zip_entry, $filesize);
										file_put_contents($dir.$filename,$contents);
            							}
        						}
   					}




		} 
	else 
		{
			echo "There was a problem saving the file. <br />";
		}

}
} 

 

The idea is, the user enters a file name and upload there zip file, once uploaded this then unzips to a specified directory.  This works fine if all the files are contained within the root of the zip, however if I try to upload a zip that contains folders then I just get Error: 1 displayed.

 

I removed the unzip function and this is happening with just the upload.

 

I would have thought that php would just copy the zip byte by byte without investigating its structure?

 

I have tested my unzip with zips that contain folders and it works fine, maintains structure and puts the files where it should, its just the upload error that is giving me problems! PHP.ini is also set to 250mb

 

Thanks for any insights

Link to comment
Share on other sites

An upload error value of 1 indicates -

Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini

 

In your previous thread, someone kindly suggested -

Please everyone, check that your file(s) uploaded successfully before you attempt to use any of the uploaded file information.

 

Since you got an upload error, the upload didn't work and the ['size'] value you are testing in your program logic is a zero, so of course it passes your if ($_FILES['file']['size'] > 262144000) test.

 

Have you tested using a phpinfo statement that when you change the php,ini upload_max_filesize setting that it actually got changed, in case the php.ini that you are changing is not the one that php is using or you used syntax in the setting that is not valid (i.e. actually putting the 'b' in 250mb is invalid and does not work)? Did you also change the php.ini post_max_size setting to at least the same value?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.