Jump to content

[SOLVED] Zip creation error?


lewis987

Recommended Posts

Im trying to make a download site that always zips files, I can get the part for individual files to work flawlessly. The problem is (im using a music folder as an example) that when the user clicks the "download album" button. The function below creates the username and is meant to open or create (dependant if the file exists). The error is that its not creating the file at all. But the same code for the individual file works. Im confused, both functions are below so you can see what each function does.

 

*NOTE*

send_File is MEANT to use $_GET and send_Folder is meant to use $_POST;

$folder returns what I want it to;

 

 

send_File():

	function send_File(){
		global $LOCATION;
		global $file_types;
		global $dir;
		global $temp_dir;
		global $GET_VAR_3;
		global $GET_VAR_2;
		global $GET_VAR_1;
		global $epoch;

		$folder = $LOCATION.urldecode($_GET[$GET_VAR_1])."/".urldecode($_GET[$GET_VAR_2])."/";					

		///File ext///
		$EXPLODe = explode(".",$_GET[$GET_VAR_3]);
		$last = count($EXPLODe) - 1;
		$file_ext = strtolower($EXPLODe[$last]);

		if($file_type[0] == "ANY"){
			$ok = 1;
		}else{
			//Check file ext:
			for($i = 0; $i < count($file_types); $i++){
				if($file_ext == $file_types[$i]){
					$ok = 1;
					$i = 5000;
				}else{
					$ok = 2;
				}
			}
		}

		if($ok == 2){
			die("<div align=\"center\"><div class=\"error\">
					Invalid file. <br />The file may exist, but the file extension is invalid.
				</div></div></body></html>");
		}

		//Zip file name:
		$file = time().urldecode($_GET[$GET_VAR_3]);
		$file = str_shuffle($file);

		//Add the extension on the zip folder. Remove ' from the name
		$zip_name = $dir.$file;
		$zip_name = str_replace("'","", $zip_name);

		$zip = new ZipArchive();
		//Set the tempory file name:
		$FILE = str_replace(" ","_",$_GET[$GET_VAR_3]);
		$TEMP = $temp_dir.$FILE.$_SERVER['remote_addr'].$epoch;

		if(!@copy($folder.$_GET[$GET_VAR_3], $TEMP)){
			die("<div align=\"center\"><div class=\"error\">
					Cannot copy file specified. <br />Please ensure it exists and that it is the directory specified.
				</div></div></body></html>");
		}

		if ($zip->open($zip_name.".zip", ZIPARCHIVE::CREATE)!==TRUE) {
			exit("cannot create file.</body></html>");
		}

		$zip->addFile($TEMP , $_GET[$GET_VAR_3]);
		$zip->close();
		unlink($TEMP);

		return $zip_name;
	}

 

send_Folder():

	function send_Folder(){
		global $LOCATION;
		global $file_types;
		global $dir;
		global $temp_dir;
		global $GET_VAR_2;
		global $GET_VAR_1;
		global $epoch;

		$folder = $LOCATION.urldecode($_POST[$GET_VAR_1])."/".urldecode($_POST[$GET_VAR_2])."/";
		$open = opendir($folder);
		$i = 0;
		while(false !== ($file = readdir($open))){
			if($file != "." || $file != ".."){
				if($file_types[0] == "ANY"){
					$files[$i] = $file;
					$i++;
				}else{
					$EXPLODe = explode(".",$file);
					$last = count($EXPLODe) - 1;
					$file_ext = strtolower($EXPLODe[$last]);
					for($o = 0; $o < count($file_types); $o++){
						if($file_ext == $file_types[$o] && is_dir($file) === FALSE){
							$files[$i] = $file;
							$o = 100000;
							$i++;
						}else{

						}	
					}
				}
			}
		}
		//Create Zip Archive:
			//Create Zip Class Here:
			$zip = new ZipArchive();

			//Make the Zip name here:
			$file = time().str_replace(" ","_", urldecode($_POST[$GET_VAR_2]));
			$file = str_shuffle($file);
			$zip_name = $dir.$file.".zip";
		//Create the zip file:
			if ($zip->open($zip_name, ZipArchive::CREATE) !== TRUE) {
				die("cannot open $zip_name.</body></html>");
			}
		//Create loop to add files and/or show error messages:
			for($i = 0; $i < count($files); $i++){
				$FILE = str_replace(" ","_",$files[$i]);
				$TEMP = $temp_dir.$FILE.$_SERVER['remote_addr'].$epoch;
				if(!@copy($folder.$files[$i], $TEMP)){
					echo("<div align=\"center\"><div class=\"error\">
							Cannot copy file specified. <br />Please ensure it exists and that it is the directory specified.
						</div></div>");
				}else{
					$zip->addFile($TEMP , $files[$i]);
				}
			}

			$zip->close();
		//Remove all the files here:
			for($i = 0; $i < count($files); $i++){
				$FILE = str_replace(" ","_",$files[$i]);
				$TEMP = $temp_dir.$FILE.$epoch;
				unlink($TEMP);
			}
		//Send the file name back to the script calling this file!
		return $zip_name;
	}

 

Any help or code optimization is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/126158-solved-zip-creation-error/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.