phpphp-ziparchive

PHP ZipArchive::addGlobe() is slow I can not delete temp directory


I have a code which

  1. Generate csv files to temp directory
  2. Call ZipArchive::addGlob()
  3. Then remove temp dir

But it seems the addGlobe() is not enough fast and files are deleted before addGlob() creates the zip file.

Code looks like:

function saveCsvDaysDataZip($campaign_id)
{
    $tempDirName = CampaignsDeviceDays::CSV_REPORTS_DIR . '/' . $campaign_id . '/temp';

    $zip = new ZipArchive();
    $zip->open($tempDirName . 'reports.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

    $addGlob = $zip->addGlob("$tempDirName/*", GLOB_BRACE, ['remove_all_path' => TRUE]);
    if( $addGlob === false ) throw  new \Exception("CampaignsDeviceDays::saveCsvDaysDataZip() error: \$zip->addGlobe() failed. " . $this->zipArchive->getStatusString() );
}


public function removeCsvExportsDir($campaign_id)
{
    $dirName = self::CSV_REPORTS_DIR . '/' . $campaign_id . '/temp';
    FileSystem::delete($dirName);
}

$saveCsvDaysDataZip($campaign_id);
$removeCsvExportsDir($campaign_id);

Is it possible that removeCsvExportsDir($campaign_id) is called before addGlob() is done?


Solution

  • Your function had no call to ZipArchive::close - which I guess PHP will then take as "permission" to continue populating the ZIP archive in the background, and return before that process is actually finished.