How to zip a folder and download it using php?

前端 未结 5 2131
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 15:01

I have folder named \"data\". This \"data\" folder contains a file \"filecontent.txt\" and another folder named \"Files\". The \"Files\" folder contains a \"info.txt\" file.

5条回答
  •  执念已碎
    2020-12-15 15:30

    Use the TbsZip class to create a new zip Archive. TbsZip is simple, it uses no temporary files, no zip EXE, it has no dependency, and has a Download feature that flushes the archive as a download file.

    You just have to loop under the folder tree and add all files in the archive, and then flush it.

    Code example:

    $zip = new clsTbsZip(); // instantiate the class
    $zip->CreateNew(); // create a virtual new zip archive
    foreach (...) { // your loop to scann the folder tree
      ...
      // add the file in the archive
      $zip->FileAdd($FileInnerName, $LocalFilePath, TBSZIP_FILE);
    }
    // flush the result as an HTTP download
    $zip->Flush(TBSZIP_DOWNLOAD, 'my_archive.zip');
    

    Files added in the archive will be compressed sequentially during the Flush() method. So your archive can contain numerous sub-files, this won't increase the PHP memory.

提交回复
热议问题