How to zip a whole folder using PHP

前端 未结 15 2254
有刺的猬
有刺的猬 2020-11-22 08:48

I have found here at stackoveflow some codes on how to ZIP a specific file, but how about a specific folder?

Folder/
  index.html
  picture.jpg
  important.tx         


        
15条回答
  •  北海茫月
    2020-11-22 09:20

    Use this is working fine.

    $dir = '/Folder/';
    $zip = new ZipArchive();
    $res = $zip->open(trim($dir, "/") . '.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
    if ($res === TRUE) {
        foreach (glob($dir . '*') as $file) {
            $zip->addFile($file, basename($file));
        }
        $zip->close();
    } else {
        echo 'Failed to create to zip. Error: ' . $res;
    }
    

提交回复
热议问题