PHP 7 ZipArchive::OVERWRITE 不工作
PHP 7 ZipArchive::OVERWRITE not working
这可能是新版本中的一个错误...或者可能某些东西改变了 ZipArchive 的行为并且我的代码只是旧的,但是,以下代码在使用 CREATE 标志时有效,但在使用 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 the ZipArchive::OVERWRITE
, the method also created non-existing archives.
Since PHP 5.6 the OVERWRITE
flag alone cannot create new archives which breaks compatibility.
这可能是新版本中的一个错误...或者可能某些东西改变了 ZipArchive 的行为并且我的代码只是旧的,但是,以下代码在使用 CREATE 标志时有效,但在使用 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.