How To compress all files within a directory using PHP

Avinash singh | 8:20 AM

How To compress all files within a directory using PHP



To do this create a "zipcode.php" file where you want to create the zip file

$zip = new ZipArchive();
$zip->open('example.zip',  ZipArchive::CREATE);
$srcDir = "/public_html/example/test/";
$files= scandir($srcDir);
//var_dump($files);
foreach ($files as $file) {
  $zip->addFromString(basename($file),  file_get_contents($srcDir.$file));  
}
$zip->close();

This will create a zip file with the name example.zip where the zipcode.php is placed
Example.zip contains all the file within the test directory







Avinash Kumar Singh © 2013. All Rights Reserved | Powered by-Blogger