简体   繁体   中英

add zip won't do anything

I am trying to zip some folders with this function:

public function generate_zip($directory,$name_of_the_folder){
        $rootPath = realpath($directory);
        $zip = new ZipArchive();
        $zip->open('path/to/my/zip/compressed.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
        $zip->addFile($rootPath,$name_of_the_folder);
        $zip->close();
    }

This does literally nothing. I have already checked the permissions and they seem to be correct.

When using ZipArchive, you cannot add an empty directory using ZipArchive::addFile() . You have to use ZipArchive::addEmptyDir() . For your case, I think what you want is ZipArchive::addGlob()

According to the documentation, the function 'addFile' wants two filenames: the first is the path to the local file (which you want to add to the zip) and the second (optional) argument is how you want the file to appear inside the zip.

You have a variable '$name_of_the_folder'. Does that contain the right value?

Php.net documentation: http://php.net/manual/en/ziparchive.addfile.php

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