How to zip a whole folder using PHP

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

    This is a working example of making ZIPs in PHP:

    $zip = new ZipArchive();
    $zip_name = time().".zip"; // Zip name
    $zip->open($zip_name,  ZipArchive::CREATE);
    foreach ($files as $file) {
      echo $path = "uploadpdf/".$file;
      if(file_exists($path)){
      $zip->addFromString(basename($path),  file_get_contents($path));---This is main function  
      }
      else{
       echo"file does not exist";
      }
    }
    $zip->close();
    

提交回复
热议问题