hy all, I want to make code for download multiple file in php so I make in zip file. my code work and can create sip file but when I want to open the zip file appear error message like this " the archive is either is unknown format or damaged ". I already attach the error message

this is my full code

function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
	$zip = new ZipArchive();
	//create the file and throw the error if unsuccessful
	if ($zip->open($archive_file_name, ZipArchive::CREATE )!==TRUE) {
    	exit("cannot open <$archive_file_name>\n");
	}
	else {
	$zip->open($archive_file_name, ZipArchive::CREATE );
	//add each files of $file_name array to archive
	foreach($file_names as $files)
	{
  		$zip->addFile($file_path.$files,$files);
		//echo $file_path.$files,$files."<br />";
	}
	$zip->close();
	//then send the headers to foce download the zip file
	header("Content-type: application/zip"); 
	header("Content-Disposition: attachment; filename=$archive_file_name"); 
	header("Pragma: no-cache"); 
	header("Expires: 0"); 
	readfile("$archive_file_name");
	exit;
	}
}

then here I call the function

$file_names = array('makeZipinPHP.jpg','Speed_Kills.wav','tick.jpg','webinfopedia.com.txt');
	$archive_file_name=$name."$ico.zip";
	$file_path=$_SERVER['DOCUMENT_ROOT'].'/download/';
	zipFilesAndDownload($file_names,$archive_file_name,$file_path);

can some one give example about this problem? I get stack with my project because of this

thank you in advance

Recommended Answers

All 4 Replies

The Content-Length header is missing for one. Search this forum, there are some threads about this.

The Content-Length header is missing for one. Search this forum, there are some threads about this.

I already try to put Content-Length header, the problem still same only the size of zip file going bigger. this is my header now

header('Content-Description: File Transfer');
		header('Content-Type: application/octet-stream');
		header('Content-Disposition: attachment; filename='.basename($archive_file_name));
		header('Content-Transfer-Encoding: binary');
		header('Expires: 0');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Pragma: public');
		header('Content-Length: ' . filesize($archive_file_name));
		ob_clean();
		flush();
		readfile($archive_file_name);

I think the problem is not in the header but when creating zip file. but I am not sure about that. Can u give me some advice?

I don't know why you open your zip file twice, and why you pass $files as the second parameter to addFile. I suggest you add some error checking to the creation of your zip file.

hy all, I want to make code for download multiple file in php so I make in zip file. my code work and can create sip file but when I want to open the zip file appear error message like this " the archive is either is unknown format or damaged ". I already attach the error message

this is my full code

function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
	$zip = new ZipArchive();
	//create the file and throw the error if unsuccessful
	if ($zip->open($archive_file_name, ZipArchive::CREATE )!==TRUE) {
    	exit("cannot open <$archive_file_name>\n");
	}
	else {
	$zip->open($archive_file_name, ZipArchive::CREATE );
	//add each files of $file_name array to archive
	foreach($file_names as $files)
	{
  		$zip->addFile($file_path.$files,$files);
		//echo $file_path.$files,$files."<br />";
	}
	$zip->close();
	//then send the headers to foce download the zip file
	header("Content-type: application/zip"); 
	header("Content-Disposition: attachment; filename=$archive_file_name"); 
	header("Pragma: no-cache"); 
	header("Expires: 0"); 
	readfile("$archive_file_name");
	exit;
	}
}

then here I call the function

$file_names = array('makeZipinPHP.jpg','Speed_Kills.wav','tick.jpg','webinfopedia.com.txt');
	$archive_file_name=$name."$ico.zip";
	$file_path=$_SERVER['DOCUMENT_ROOT'].'/download/';
	zipFilesAndDownload($file_names,$archive_file_name,$file_path);

can some one give example about this problem? I get stack with my project because of this

thank you in advance

Finally I already solve this problem I change the zip function and add 1 class

this is my file now
hope can help.

<?php

session_start();
require("init.php");
$fungsi = new fungsi;



class zipfile
{
var $datasec = array();
var $ctrl_dir = array();
var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
var $old_offset = 0;

function add_dir($name) {
$name = str_replace("", "/", $name);

$fr = "\x50\x4b\x03\x04";
$fr .= "\x0a\x00";
$fr .= "\x00\x00";
$fr .= "\x00\x00";
$fr .= "\x00\x00\x00\x00";

$fr .= pack("V",0);
$fr .= pack("V",0);
$fr .= pack("V",0);
$fr .= pack("v", strlen($name) );
$fr .= pack("v", 0 );
$fr .= $name;
$fr .= pack("V", 0);
$fr .= pack("V", 0);
$fr .= pack("V", 0);

$this -> datasec[] = $fr;
$new_offset = strlen(implode("", $this->datasec));

$cdrec = "\x50\x4b\x01\x02";
$cdrec .="\x00\x00";
$cdrec .="\x0a\x00";
$cdrec .="\x00\x00";
$cdrec .="\x00\x00";
$cdrec .="\x00\x00\x00\x00";
$cdrec .= pack("V",0);
$cdrec .= pack("V",0);
$cdrec .= pack("V",0);
$cdrec .= pack("v", strlen($name) );
$cdrec .= pack("v", 0 );
$cdrec .= pack("v", 0 );
$cdrec .= pack("v", 0 );
$cdrec .= pack("v", 0 );
$ext = "\x00\x00\x10\x00";
$ext = "\xff\xff\xff\xff";
$cdrec .= pack("V", 16 );
$cdrec .= pack("V", $this -> old_offset );
$cdrec .= $name;

$this -> ctrl_dir[] = $cdrec;
$this -> old_offset = $new_offset;
return;
}

function add_file($data, $name) {
$fp = fopen($data,"r");
$data = fread($fp,filesize($data));
fclose($fp);
$name = str_replace("", "/", $name);
$unc_len = strlen($data);
$crc = crc32($data);
$zdata = gzcompress($data);
$zdata = substr ($zdata, 2, -4);
$c_len = strlen($zdata);
$fr = "\x50\x4b\x03\x04";
$fr .= "\x14\x00";
$fr .= "\x00\x00";
$fr .= "\x08\x00";
$fr .= "\x00\x00\x00\x00";
$fr .= pack("V",$crc);
$fr .= pack("V",$c_len);
$fr .= pack("V",$unc_len);
$fr .= pack("v", strlen($name) );
$fr .= pack("v", 0 );
$fr .= $name;
$fr .= $zdata;
$fr .= pack("V",$crc);
$fr .= pack("V",$c_len);
$fr .= pack("V",$unc_len);

$this -> datasec[] = $fr;
$new_offset = strlen(implode("", $this->datasec));

$cdrec = "\x50\x4b\x01\x02";
$cdrec .="\x00\x00";
$cdrec .="\x14\x00";
$cdrec .="\x00\x00";
$cdrec .="\x08\x00";
$cdrec .="\x00\x00\x00\x00";
$cdrec .= pack("V",$crc);
$cdrec .= pack("V",$c_len);
$cdrec .= pack("V",$unc_len);
$cdrec .= pack("v", strlen($name) );
$cdrec .= pack("v", 0 );
$cdrec .= pack("v", 0 );
$cdrec .= pack("v", 0 );
$cdrec .= pack("v", 0 );
$cdrec .= pack("V", 32 );
$cdrec .= pack("V", $this -> old_offset );

$this -> old_offset = $new_offset;

$cdrec .= $name;
$this -> ctrl_dir[] = $cdrec;
}

function file() {
$data = implode("", $this -> datasec);
$ctrldir = implode("", $this -> ctrl_dir);

return
$data .
$ctrldir .
$this -> eof_ctrl_dir .
pack("v", sizeof($this -> ctrl_dir)) .
pack("v", sizeof($this -> ctrl_dir)) .
pack("V", strlen($ctrldir)) .
pack("V", strlen($data)) .
"\x00\x00";
}

function is_bot()
{
	//This function will check whether the visitor is a search engine robot 

	$botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi",
	"looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory",
	"Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot",
	"crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp",
	"msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz",
	"Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot",
	"Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot",
	"Butterfly","Twitturls","Me.dium","Twiceler");

	foreach($botlist as $bot)
	{
		if(strpos($_SERVER['HTTP_USER_AGENT'],$bot)!==false)
		return true;	// Is a bot
	}

	return false;	// Not a bot
}

	function counter($data)
	{
		global $db;
	
				$r = $db->select("SELECT DOWNLOADED FROM `arpt_download` WHERE `ICAO` =  '".$data."' ");
				$row = $db->get_row($r, 'MYSQL_ASSOC');
				$max = $row["DOWNLOADED"] + 1;
				//echo $downloaded1;
				//echo "<br>";
				//echo $max;
				$db->update_sql("update arpt_download set DOWNLOADED = '".$max."' where ICAO = '".$data."'");
	}

}

$zipTest = new zipfile();
		if (!$zipTest->is_bot()){
				
				$loginName = $_SESSION['user_name'];
				$ico = $_GET['ICAO'];
		
		
				$sql = "select d.FILE_NAME_NEW from log_customer_user a left join log_customer_info b ON a.id_customer = b.ID left join log_detail_arpt c ON b.ID = c.AIRLINE_ID left join lib_pdf_mgt d ON c.ARPT_ID=d.ARPT_IDENT left join arpt e ON d.ARPT_IDENT = e.ARPT_IDENT left join country f ON e.CTRY = f.IDENT WHERE a.id_customer = ".$loginName." AND c.ICAO=".$ico."";
				$r = $db->select($sql);
				
				while ($row = $db->get_row($r, 'MYSQL_ASSOC'))
				{
				
				// Test this class
				//$zipTest = new zipfile();
				$zipTest->add_dir("NAVIGA/");
				$zipTest->add_file("../download/".$row[FILE_NAME_NEW]."", "NAVIGA/".$row[FILE_NAME_NEW]."");
				//$zipTest->add_file("../download/logooo.jpg", "images/logooo.jpg");
				//$zipTest->add_file("(Computing)(1).pdf", "download(Computing)(1).pdf");
				}
				
				$zipTest->counter($ico);



				header('Content-Description: File Transfer');
				header('Content-Type: application/octet-stream');
				Header ("Content-disposition: attachment; filename=".$ico.".zip");
				header('Content-Transfer-Encoding: binary');
				header('Expires: 0');
				header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
				header('Pragma: public');
				ob_clean();
				flush();
				echo $zipTest->file();
				


}
?>
in here I also put counter to how much times the file already downloaded
commented: Thanks for sharing. +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.