Search code examples
phpphp-ziparchive

Issue with zipArchive::close() returning false


So zipArhive:: is returning false, I have tried the getStringStatus and it is returning "no error" so i'm stuck on what to do...

$fp = fopen($filePath.$fileName, 'w');
$xml = $carXML->asXML();

$zipFileName = date('dmY')."-ebay.zip";

$zip = new ZipArchive();

$r= file_exists($filePath.$fileName);
var_dump($r);
$r = $zip->open($zipFileName, ZipArchive::CREATE);
var_dump($r);

$r = $zip->addFile($filePath.$fileName);
var_dump($r);

$r = $zip->getStatusString();
var_dump($r);

$r = $zip->close();
var_dump($r);

fputs ($fp, $xml);
fclose($fp);

Results:bool(true) bool(true) bool(true) string(8) "No error" bool(false)


Solution

  • You're opening a file for writing but haven't created it by the time you add it to the zip archive. What happens if you create the $fp file (and close it) before trying to add it to the ZipArchive?

    <?php 
    
    $xmlFileName = $filePath.$fileName;
    $fp = fopen($xmlFileName, 'w');
    $xml = $carXML->asXML();
    fputs ($fp, $xml);
    fclose($fp);
    
    $zipFileName = date('dmY')."-ebay.zip";
    
    $zip = new ZipArchive();
    
    $r = $zip->open($zipFileName, ZipArchive::CREATE);
    $r = $zip->addFile($xmlFileName);
    $r = $zip->close();
    
    var_dump($r);