troubleshooting Question

How to make ZipArchive work as I want

Avatar of Richard Korts
Richard KortsFlag for United States of America asked on
PHP* zipped files
1 Comment1 Solution50 ViewsLast Modified:
I have this code to create a zip file.

$path = "zips/z" . $pid . ".zip";
$dels = array();
$nd = 0;
if ($zip->open($path, ZipArchive::CREATE) === TRUE)
{
    // Add files to the zip file
	for ($i = 0; $i < $ns; $i++) {
		$typ = substr($fs[$i],0,2);
		if($typ == "NP" || $typ == "NT" || $typ == "RU" || $typ == "TR") {
			$dels[$nd] = $fs[$i];
			$nd++;
			$str = "../pselsumms/" . $fs[$i];
			$ctfile = "temp/" . $fs[$i];
			copy ($str, $ctfile);
			$zip->addFile($ctfile);
		} else {
			$zip->addFile($fs[$i]);
		}	
    }
    // All files are added, so close the zip file.
    $zip->close();
	// delete temp files here
	/*if ($nd != 0) {
		for ($j = 0; $j < $nd; $j++) {
			$dfile = "temp/" . $dels[$i];
			unlink ($dfile);
		}	*/
	// download
	header('Content-type: application/force-download'); 
    header('Content-Transfer-Encoding: Binary'); 
    header('Content-length: ' . filesize($path)); 
    header('Content-disposition: attachment; filename=' . basename($path)); 
    readfile($path);
}

It appears to work fine, except when the name of the zip file already exists (from an earlier time).

For example, if $pid is 3512 and file zips/z3512.zip already exists, it does not overwrite the existing file, it just changes the date, but the content is as before, NOT the new content. I could of course write code to insure a unique name, based on what is already there, but why is that necessary.

FYI, I tried php unlink on the file, no dice, same thing happens.

Thanks
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros