ZipArchive Close Warning

I am trying to fix a problem in an auto-zip script for some images that I wrote a while ago, and this has worked so far. Everything seems fine to $zip->close(); which gives the following:

  <b>Warning</b>: ZipArchive::close(): Read error: No such file or directory in <b></b> on line <b>287</b><br /> 

I read the docs and some forums and found out that this could happen in one of the following scenarios:

  • If the actual files have not been added to the zip, since PHP 5.6, this may be the likely explanation, since I recently upgraded to PHP 5.6. But:
    • I verify that every file exists before adding it
    • I tried adding a dummy nonempty text file to zip. Adding it to zip returns true, like file_exists() in a file
    • When I echo $zip->numFiles and it gives a number of at least 1 (when zip has no files except the dummy)
  • If the directory in which the zip should be written does not exist or does not have access rights: this does not seem to be the case, but to be sure, I wrote a text file for the same folder in the same script and there were no problems
  • If there is a problem writing to the temp directory. This is a bit more difficult to verify, but I downloaded the script to the same system as it works, and I made sure there were no problems with disk space, etc.

Here is the relevant code. Some variables are predefined. Please note that I write every problem in my log and this script does not generate any records!

 $zip_file = 'Project'.$project_id.'.zip'; $zip = new ZipArchive; if ($zip_result = $zip->open($zip_path.'/'.$zip_file, ZIPARCHIVE::CREATE) !== true) { echo 'Error creating zip for project: '.$project_id.'. Error code: '.$zip_result; help::debugLog('Error creating zip for project: '.$project_id.'. Error code: '.$zip_result); return false; } $file_list = array(); foreach ($item_thumbs as $item) { $full_thumb_path = $thumb_dir.'/'.$item['thumb']; if (file_exists($full_thumb_path) and $item['thumb']) { $file_added = $zip->addFile($full_thumb_path, basename($item['thumb'])); if (!$file_added) help::debugLog('Failed to add item thumb to project zip. Project: '.$project_id.', file name: '.$item['thumb']); else $file_list[] = $item['thumb']; } elseif ($item['thumb']) /* If thumb indicated in DB doesn't exist in file system */ help::debugLog('Item thumb file '.$item['thumb'].' from item: '.$item['id'].' is missing from its indended location: '.$full_thumb_path); } /* Added 2016-05-18 -- creates dummy file for the zip listing its contents, important in case zip is empty */ $file_list_path = $zip_path.'/file_list.txt'; if (!($file_list_file = fopen($file_list_path, 'w+'))) help::debugLog('Failed to create list file (intended for zip) for project: '.$project_id); fwrite($file_list_file, "File list:\n"); fwrite($file_list_file, implode("\n", $file_list)); if (file_exists($file_list_path)) { fclose($file_list_file); if (!$zip->addFile($file_list_path)) help::debugLog('Failed to add list file to project zip for project: '.$project_id); unlink($file_list_path); } else help::debugLog('Failed to create list file (intended for zip) for project: '.$project_id); $zip->close(); // line 287 
+5
source share
2 answers

It turns out the solution was very simple, and in fact it was mentioned in the docs (php.net) in one of the comments:

This might seem a little obvious to some, but it was overseeing me.

If you are adding files to the zip file that you want to delete, be sure to delete AFTER you call the close () function.

If the files added to the object are not available during saving, the zip file will not be created.

So, from my code above, the text file "dummy" is deleted before closing the zip, which necessarily makes it impossible for the zip file.

I had good reason to believe that the zip was created in a temporary place and only moved to the final location on close() . It turns out that this is not so.

+6
source

Check the value of $ full_thumb_path in the line

  $file_added = $zip->addFile($full_thumb_path, basename($item['thumb'])); 

The value should be the path to the file and there should not be the path to the directory.

-1
source

All Articles