简体   繁体   中英

Corrupt ZIP files using PHP & PCLZip

I'm having some issues, I'm using PCLZip to create an archive. I don't get any errors, the zip file is created, but when I go to view it, the archive is empty and on my windows machine I get an error "The Compressed (zipped) folder "local directory zip file") is invalid. I have the following code:

$dir = '../downloads/liability/';
$archive = new PclZip($dir.'archive.zip');
$v_list = $archive->create($dir);
    if ($v_list == 0) {
        die("Error : ".$archive->errorInfo(true));
    }

My directory structure is:

-admin
  --liabilityDev.php (where the above code resides)
  --index.php
  --commission.php
-downloads
  --liability
    ---one.pdf
    ---two.pdf

The end result is that in the liability folder, there is a file called archive.zip which contains the 2 pdf's but I get the invalid error. If I don't have the directory variable, I archive index.php and commission.php and that works fine. It leads me to believe it may be a permission issue, but I'm running on fumes now. Please help!

You can try this:

if(extension_loaded('zip')){
 $zip = new ZipArchive();
 if($zip->open('../downloads/liability/archive.zip', ZIPARCHIVE::CREATE)===TRUE){
   $zip->addFile('path of any normal file to be add into zip');  
 }
 $zip->close();
}  

I think, this will fullfill your need. Before implementing this code, please check first that, zip extension is already loaded or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM