Jump to content

"Invalid or unitialized Zip object"


haydnw

Recommended Posts

Hi everyone,

 

I have some code which has been successfully zipping up the contents of a folder full of images. However, whilst playing around with some other things on the site, I seem to have broken it! The error message I get is...

 

<b>Warning</b>:  ZipArchive::addFile() [<a href='ziparchive.addfile'>ziparchive.addfile</a>]: Invalid or unitialized Zip object in <b><server path to my file>/gallery/includes/classes/hwGallery.php</b> on line <b>195</b><br />

 

Could anyone please explain what might be causing this? I'm sure the object is being initialised OK, as it's exactly the same code as previously, which worked in the past, although now even that seems to have broken (i.e. uploading an old version of the code, which used to work, also now fails). Could this be a file permissions error? I'm not entirely sure where PHP would be writing the file (does it even do that, or is it all carried out in memory?). Any pointers or helpers would be gratefully received - Google and the search feature on this site haven't turned up anything so far.

 

Thanks,

Haydn

 

Code:

The code to get the zip file is part of the 'hwGallery' class...

/*
 * Create a ZIP file containing all the images in the gallery
 * 
 */
public function getZipFile() {

	// Partially taken from http://ubuntuforums.org/showthread.php?t=859477

	// Zip file header
	header("Content-Type: application/zip");
	header('Content-Disposition: attachment; filename="gallery.zip"');

	// Create actual Zip file
	$zipFile = new ZipArchive();
	$zipFile->open("download.zip", ZipArchive::OVERWRITE);
	foreach ($this->imagePaths as $imageFile) {
		if (strpos($imageFile,"index") == false) {
			$zipFile->addFile($imageFile);
		}
	}
	$zipFile->close();

	// Return the ZIP file, then delete it
	echo file_get_contents("download.zip");
	unlink("download.zip");

}

 

which is called in the following manner:

 

$gallery = new hwGallery($id);
$gallery->getZipFile();

Link to comment
Share on other sites

  • 5 weeks later...
  • 2 years later...

I am just posting this as I hate when I find these so high on google without a resolution.

 

To fix this, you must specify the flags as create or overwrite.

$zipFile->open("download.zip", ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);

 

Example:

	function zip_magento() {
	$zip = new ZipArchive() ;
	$opened = $zip->open($this->export_dir.'client_'.$this->uniqueID.'/all.zip', ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE );
	if( $opened !== true ){
		echo("cannot open $filename for writing.");
    }
	//if ($zip->open($this->export_dir.'client_'.$this->uniqueID.'/all.zip',ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== TRUE) {
	//	echo ("Could not create archive");
	//}
	foreach($this->files as $k=>$data){
		$zip->addFile($data);
	}
	$zip->close();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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