Search code examples
phpphp-ziparchive

ZipArchive::CREATE not working


I am trying to test the ZipArchive function within my application and for some reason I'm not getting anything.

See my vardump

object(ZipArchive)#1 (5) { ["status"]=> int(0) ["statusSys"]=> int(0)
 ["numFiles"]=> int(0) ["filename"]=> string(31) 
"E:\xampp\htdocs\test\zipfile.zip" ["comment"]=> string(0) "" }

My PHP

$zip = new ZipArchive();
$zip->open('zipfile.zip', ZipArchive::CREATE);

if($zip === TRUE){

} else {
    var_dump($zip);
}

When checking the destination I am not getting any zip file, I have also checked to see if the extension is activated and I can confirm it is.


Solution

  • For me it appeared that everything was ok but the file was not generated until I added some content.

    <?php
        $zf=__DIR__ . '\zipfile.zip';
    
        /* To create a simple zip archive */
        $zip = new ZipArchive;
        $res = $zip->open( $zf, ZipArchive::CREATE );
    
        if( $res === TRUE ){
            $zip->addFromString('test.txt', 'This is some dummy data - without one file this seems to fail');
            $zip->close();
        } else {
            var_dump( $zip );
        }
    
    
        /* One approach to zip a directory */       
        $dir=__DIR__;
        $files=glob( $dir . '\*.*' );
    
        $zip = new ZipArchive;
        $res = $zip->open( $zf, ZipArchive::CREATE );
        if( $res===true ){
            foreach( $files as $file )  $zip->addFile( $file, basename( $file ) );
            $zip->close();
        }
    ?>