Dev.W
Dev.W

Reputation: 2360

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.

Upvotes: 1

Views: 943

Answers (1)

Professor Abronsius
Professor Abronsius

Reputation: 33823

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

Upvotes: 1

Related Questions