2016-08-29 6 views
2

Dies könnte ein Fehler in der neuen Version ... oder vielleicht etwas hat das Verhalten von ZipArchive geändert und mein Code ist nur alt, aber der folgende Code funktioniert, wenn die CREATE-Flag ist verwendet, bricht jedoch, wenn das OVERWRITE-Flag verwendet wird. Es funktionierte gut unter PHP 5.6, aber unter PHP 7.0 bekomme ich folgende Fehlermeldung:PHP 7 ZipArchive :: OVERWRITE funktioniert nicht

Warning: ZipArchive::close(): Invalid or uninitialized Zip object 

Originalcode:

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(); 
} 

Irgendwelche Ideen?

Antwort

6

Übergeben Sie ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE als die Flagge.

Es ist ein Fehler: https://bugs.php.net/bug.php?id=71064 (seit 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.

+0

Works! Ich dachte, du müsstest nur den einen oder anderen passieren. – photocode

+1

@photocode siehe aktualisierte Antwort. Es ist ein Fehler. – Gordon