Tuesday 8 July 2014

How to zipp the folder/directory in php


Before Starting: 

 Know About File Location

  • This is my localhost side location where my index.php stand : 
'C:\xampp\htdocs\filetransfer\index.php
  • This is my  localhost side directory file which i want to zip :
 'C:\xampp\htdocs\filetransfer\zipfolder' 
  • This is my  localhost side directory location where i want to zip : 
'C:\xampp\htdocs\filetransfer\create\new\folder\abc.zip';                                                                      
<?php
class FlxZipArchive extends ZipArchive {
 public function addDir($location, $name){
 $this->addEmptyDir($name);
 $this->addDirDo($location, $name);
 }
 private function addDirDo($location, $name) {
$name .= '/';
$location .= '/';
$dir = opendir ($location);
while ($file = readdir($dir)){
if ($file == '.' || $file == '..') continue;
$do = (filetype( $location . $file) == 'dir') ?  'addDir'  :'addFile';
$this->$do($location . $file, $name . $file);
}

}
$the_folder = 'C:\xampp\htdocs\filetransfer\zipfolder'  ;
$zip_file_name='C:\xampp\htdocs\filetransfer\create\new\folder\abc.zip;'                                                                       
$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
if($res === TRUE)
{
$za->addDir($the_folder, basename($the_folder));
$za->close();
}
else{ echo 'Could not create a zip archive';}
?>



1 comment: