简体   繁体   中英

php zip archive

I tried to create empty/not empty zip archive.
Code used is below.

Result: nothing happened.
No errors, but also no results.

Any explanation appreciated.

$zip = new ZipArchive();
$zip->open('/home/admin/domains/domain.com/public_html/xxx_zip.zip', ZIPARCHIVE::CREATE);
$zip->addFile('/home/admin/domains/domain.com/public_html/xxxxxx.css');
#$zip->addEmptyDir('.'); //also tried
$zip->close();

available
Zip enabled
Extension Version $Id: php_zip.c,v 1.1.2.50 2009/03/01 17:35:25 iliaa Exp $
Zip version 1.8.11
Libzip version 0.9.0

The ZipArchive methods indicate failure by returning false. Did you check that? Did you check getStatusString ?

check out other examples but this is how I got it working:

    <?php

        $dirArray = array();

        /* creates a compressed zip file */
        $zip = new ZipArchive;
        if ($zip->open('dataminefiles.zip', ZIPARCHIVE::CREATE) !== TRUE) {
            die ("Could not open archive"); 
        }
        // open the current dir
        if ($handle = opendir('.')) {
        while (false !== ($entry = readdir($handle))) {
            // ignore hidden files          
            if ($entry != "." && $entry != "..") {
            // only zip specific files
                if ( substr($entry,-3,3) == "jpg" || substr($entry,-3,3) == "pdf" || substr($entry,-3,3) == "lsx" || substr($entry,-3,3) == "xls" || substr($entry,-3,3) == "doc" || substr($entry,-3,3) == "txt" || substr($entry,-3,3) == "png" || substr($entry,-3,3) == "gif" || substr($entry,-3,3) == "peg" ) {
                    // if allowed, add them to the array
                    $dirArray[] = $entry;
                }
            }
        }
        closedir($handle);
    }

        $indexCount = count($dirArray);
        sort($dirArray);
            // loop through the files and add them to the zip file
        for($index=0; $index < $indexCount; $index++) {
                $file = "{$dirArray[$index]}";
                $zip->addFile($file, $file);
        }
    // close the zip file
        $zip->close();

    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM