How to Zip a directory in PHP?

PHPProgrammingServer Side Programming

We can use PHP ZipArchive class in order to zipping and unzipping the folder in PHP. As of PHP 5.3, this class is inbuilt. For using in windows users need to enable php_zip.dll inside of php.ini.

Example

<?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();
   }
?>
raja
Published on 27-Jun-2019 15:58:16
Advertisements