Computer >> Máy Tính >  >> Lập trình >> PHP

Làm thế nào để Zip một thư mục trong PHP?


Chúng ta có thể sử dụng lớp PHP ZipArchive để nén và giải nén thư mục trong PHP. Kể từ PHP 5.3, lớp này được tạo sẵn. Để sử dụng trong windows, người dùng cần bật php_zip.dll bên trong php.ini.

Ví dụ

<?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();
   }
?>