Warning on ZipArchive Close

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.


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().

Tags:

Php

Ziparchive