How to zip a whole folder using PHP

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

    There is a useful undocumented method in the ZipArchive class: addGlob();

    $zipFile = "./testZip.zip";
    $zipArchive = new ZipArchive();
    
    if ($zipArchive->open($zipFile, (ZipArchive::CREATE | ZipArchive::OVERWRITE)) !== true)
        die("Failed to create archive\n");
    
    $zipArchive->addGlob("./*.txt");
    if ($zipArchive->status != ZIPARCHIVE::ER_OK)
        echo "Failed to write files to zip\n";
    
    $zipArchive->close();
    

    Now documented at: www.php.net/manual/en/ziparchive.addglob.php

提交回复
热议问题