这可能是新版本中的错误...或者可能某些东西改变了 ZipArchive 的行为并且我的代码只是旧的,但是,以下代码在使用 CREATE 标志时有效,但在 OVERWRITE 标志时中断用来。它在 PHP 5.6 下运行良好,但在 PHP 7.0 下出现以下错误:
Warning: ZipArchive::close(): Invalid or uninitialized Zip object
原代码:
foreach( glob($sourcedir.'*.[zZ][iI][pP]') as $zippath)
{
// create daily zip file
$zipname = preg_replace('~'.$sourcedir.'~','',$zippath);
$zipname2 = preg_replace('~\.zip~','',$zipname);
$zip = new ZipArchive();
$ret = $zip->open($xmlzip.$zipname2.'_contact_xml.zip', ZipArchive::OVERWRITE);
// move xml files to daily zip file created above
if ($ret !== TRUE) {
printf('Failed with code %d', $ret);
} else {
foreach(glob($source_file_path.'*.[xX][mM][lL]') as $xmlpath){
$zip->addFile($xmlpath, preg_replace('~'.$source_file_path.'~','',$xmlpath));
}
}
$zip->close();
}
有什么想法吗?
最佳答案
通过 ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE
作为标志。
这是一个错误:https://bugs.php.net/bug.php?id=71064 (自 PHP 5.6.16 起)
There is an issue with the ZipArchive class'
open()
method. In previous versions of PHP when the only flag passed to the method was theZipArchive::OVERWRITE
, the method also created non-existing archives.Since PHP 5.6 the
OVERWRITE
flag alone cannot create new archives which breaks compatibility.
关于PHP 7 ZipArchive::OVERWRITE 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39198950/