Avatar of Richard Korts
Richard Korts
Flag for United States of America asked on

How to make ZipArchive work as I want

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);
}

Open in new window


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
* zipped filesPHP

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck