Warning on ZipArchive Close
<p>I am trying to fix an issue in an auto-zip script for some images, which I wrote a while ago and it worked until now. Everything seems fine until <code>$zip-&gt;close();</code> which gives the following:</p>

<pre><code> &lt;b&gt;Warning&lt;/b&gt;:  ZipArchive::close(): Read error: No such file or directory in &lt;b&gt;&lt;/b&gt; on line &lt;b&gt;287&lt;/b&gt;&lt;br /&gt;
</code></pre>

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

<ol>
<li>If no actual files are added to the zip, since PHP 5.6—this could be a likely explanation since I recently upgraded to PHP 5.6. However:

<ul>
<li>I check that each file exists before adding it</li>
<li>I tried to add a dummy non-empty text file to the zip. Adding it to the zip returns true, as does <code>file_exists()</code> on the file</li>
<li>When I echo <code>$zip-&gt;numFiles</code> and it gives a number of at least 1 (when the zip has no files except the dummy)</li>
</ul></li>
<li>If the directory to where the zip needs to be written doesn't exist or doesn't have the right permissions: This didn't appear to be the case, but just to be sure, I wrote a text file to the same folder in the same script and there was no problem</li>
<li>If there is a problem writing to the temp directory. It's a bit harder to check this but I have an upload script in the same system that works, and I made sure that there are no disk space problems etc.</li>
</ol>

<p>Here is the relevant code. Some variables are defined beforehand. Note that I write every problem to my log, and this script doesn't generate any entries!</p>

<pre><code>$zip_file = 'Project'.$project_id.'.zip';
$zip = new ZipArchive;
if ($zip_result = $zip-&gt;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-&gt;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-&gt;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-&gt;close(); // line 287
</code></pre>