Zip a Directory in PHP



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();
   }
?>
Updated on: 2020-06-29T11:47:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements