Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $folder_name = '';
- $root_path_upto_folder = '';
- $rootpath = getcwd();
- if(DIRECTORY_SEPARATOR!=='/'){
- $rootpath = str_replace(DIRECTORY_SEPARATOR,'/',$rootpath);
- }
- if(!empty($rootpath)){
- $arr = explode('/', $rootpath);
- if(!empty($arr)){
- $folder_name = $arr[count($arr)-1];
- array_pop($arr);
- if(!empty($arr)){
- $root_path_upto_folder = implode('/', $arr);
- }
- }
- }
- if(empty($folder_name) || empty($root_path_upto_folder)){
- echo 'Unable to process';
- die;
- }
- $the_folder = $root_path_upto_folder.'/'.$folder_name;
- $zip_file_name = $folder_name.'.zip';
- $download_file= true;
- #$delete_file_after_download = true;
- 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);
- }
- }
- }
- $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';
- }
- if ($download_file)
- {
- ob_get_clean();
- header("Pragma: public");
- header("Expires: 0");
- header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
- header("Cache-Control: private", false);
- header("Content-Type: application/zip");
- header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";" );
- header("Content-Transfer-Encoding: binary");
- header("Content-Length: " . filesize($zip_file_name));
- readfile($zip_file_name);
- }
- if(file_exists($the_folder.'/'.$zip_file_name)){
- unlink($the_folder.'/'.$zip_file_name);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement