Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag 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
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial