When a zip file is left empty, a warning is triggered when script end
Description
The following code:
<?php
$zip = new ZipArchive();
$out = "test.zip";
$zip->open($out, ZipArchive::CREATE | ZipArchive::OVERWRITE);
?>
Resulted in this warning:
PHP Warning: Unknown: Cannot destroy the zip context: Can't remove file: No such file or directory in Unknown on line 0
But I expected no warnings. This does not happen if flag ZipArchive::OVERWRITE is not specified.
I guess ZipArchive has a registered shutdown function to clear temp zip file when script ends, but that file is created when the first file is added.
PHP Version
PHP 7.x
Operating System
Linux Ubuntu 16.04
I cannot reproduce this; neither with PHP-8.0 nor with PHP-7.4, using libzip 1.7.1. I think this is an issue with an older libzip – which version do you use? Also note that PHP 7 is no longer actively supported, and as such won't receive bug fixes. Can you reproduce the issue with PHP 8.0.14?
Check your Apache error_log. I think it isn’t displayed in browser because it happens in the shutdown phase (note “at line 0”). I can reproduce on newer versions but libzip is pretty old (1.0.1 as shown phpinfo), so this may be the problem. Thanks
Neither Apache nor a browser involved here. This issue is not SAPI specifc.
The ZipArchive::OVERWRITE flag is actually ZIP_TRUNCATE. This flag causes an attempt to delete the archive, but of course that fails if the file does not exist. Prior to libzip 1.6.0, this was reported as the error you're experiencing. This bug has been fixed with https://github.com/nih-at/libzip/commit/23b25a8d69a8b90c08e1923e674e2ff654af5575. I don't think it makes sense for PHP to try to work around that bug, since that can be done in userland as well (just don't open non-existing archives with ZipArchive::OVERWRITE), and the bug has been fixed upstream. And of course, you could just ignore this warning.