How to zip a whole folder using PHP

前端 未结 15 2259
有刺的猬
有刺的猬 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:25

    Try this:

    $zip = new ZipArchive;
    $zip->open('myzip.zip', ZipArchive::CREATE);
    foreach (glob("target_folder/*") as $file) {
        $zip->addFile($file);
        if ($file != 'target_folder/important.txt') unlink($file);
    }
    $zip->close();
    

    This will not zip recursively though.

提交回复
热议问题