20

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 $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:

  1. 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:
    • I check that each file exists before adding it
    • I tried to add a dummy non-empty text file to the zip. Adding it to the zip returns true, as does file_exists() on the file
    • When I echo $zip->numFiles and it gives a number of at least 1 (when the zip has no files except the dummy)
  2. 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
  3. 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.

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!

$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

4 Answers 4

27

Turns out the solution was very simple, and it's actually referred to in the docs (php.net), in one of the comments by Jared at kippage dot com:

It may seem a little obvious to some but it was an oversight on my behalf.

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

If the files added to the object aren't available at save time the zip file will not be created.

(Source: https://www.php.net/manual/en/ziparchive.close.php#93322)

So from my code above, the "dummy" text file is deleted before the zip is closed, necessarily making it that the file doesn't exist at the time the zip is created.

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

2
  • 4
    So, how did you end up solving the issue in your code? Commented Nov 26, 2016 at 5:02
  • 1
    I moved the $zip->close() to a point before the text file is deleted.
    – Ynhockey
    Commented Mar 8, 2017 at 12:01
2

Just because this is #1 in Google for the error message, I'm adding another possible issue that causes this exact same error.

If you do not add any files to the zip archive, it doesn't yet exist, therefore the close() will fail on an empty archive.

eg:

$zip = new ZipArchive;
$zip->open("foo.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->close();

produces:

ERROR: ZipArchive::close(): Can't remove file: No such file or directory

So if you're looping and adding files, make sure you've added something before calling close().

2
  • Should I do any cleanup, if I realize, that no files where added? The documentation says, that close() "is automatically called at the end of the script". So how can I avoid the error message, once I have created the ZipArchive, but realize only afterwards, that there are no files to be added? Commented Dec 5, 2021 at 16:38
  • How about Warning: ZipArchive::close(): Can't open file: No such device or address I have added the zip file object(ZipArchive)[1] public 'status' => int 0 public 'statusSys' => int 0 public 'numFiles' => int 3 public 'filename' => string '/tmp/tmp.zip' (length=12) public 'comment' => string '' (length=0)
    – Anas
    Commented Nov 19, 2022 at 5:58
1

As mentioned in Ynhockey's answer the most probable reason for this is that you're deleting the file before closing the zip file. One obvious solution is to delete the files after calling $zip->close(). But here is an alternative.

Instead of doing

$zip->addFile('file.txt', 'file.txt');
unlink('file.txt');
$zip->close(); //gives error!

You can call the addFromString function and do this instead:

$zip->addFromString('file.txt', file_get_contents('file.txt'));
unlink('file.txt');
$zip->close(); //does not give any error!
-1

Check the value of "$full_thumb_path" in the line

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

The value should be a file path and mustn't be a directory path.

2
  • Even if it's a directory it wouldn't give a warning on zip_close() in this script, if the dummy file isn't deleted.
    – Ynhockey
    Commented Jul 25, 2016 at 13:25
  • I just had this during a phing build. The message: [PHP Error] ZipArchive::close(): Read error: Is a directory --> the answer is simple, it is just because there was some symbolic link in the file tree
    – Nadir
    Commented Jan 1, 2017 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.