haydnw Posted January 11, 2009 Share Posted January 11, 2009 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 https://forums.phpfreaks.com/topic/140414-invalid-or-unitialized-zip-object/ Share on other sites More sharing options...
haydnw Posted January 12, 2009 Author Share Posted January 12, 2009 Anyone? More frantic Googling still hasn't helped... Link to comment https://forums.phpfreaks.com/topic/140414-invalid-or-unitialized-zip-object/#findComment-735592 Share on other sites More sharing options...
icon33 Posted February 11, 2009 Share Posted February 11, 2009 Having the same problem - did you find a solution? Link to comment https://forums.phpfreaks.com/topic/140414-invalid-or-unitialized-zip-object/#findComment-759901 Share on other sites More sharing options...
tarcinil Posted May 5, 2011 Share Posted May 5, 2011 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 https://forums.phpfreaks.com/topic/140414-invalid-or-unitialized-zip-object/#findComment-1211133 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.