如何在 PHP 中壓縮資料夾?


我們可以使用 PHP ZipArchive 類來壓縮和解壓 PHP 中的資料夾。自 PHP 5.3 起,此類為內建類。對於 Windows 使用者,需要在 php.ini 中啟用 php_zip.dll。

示例

<?php
//Enter the name of directory
   $pathdir = "Directory Name/";
//Enter the name to creating zipped directory
   $zipcreated = "test.zip";
//Create new zip class
   $newzip = new ZipArchive;
   if($newzip -> open($zipcreated, ZipArchive::CREATE ) === TRUE) {
      $dir = opendir($pathdir);
      while($file = readdir($dir)) {
         if(is_file($pathdir.$file)) {
            $newzip -> addFile($pathdir.$file, $file);
         }
      }
      $newzip ->close();
   }
?>

更新時間: 2020 年 6 月 29 日

2 千以上次瀏覽

開啟你的 事業

完成課程獲得認證

開始
廣告