Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. <?php
  2. $folder_name = '';
  3. $root_path_upto_folder = '';
  4. $rootpath = getcwd();
  5. if(DIRECTORY_SEPARATOR!=='/'){
  6. $rootpath = str_replace(DIRECTORY_SEPARATOR,'/',$rootpath);
  7. }
  8. if(!empty($rootpath)){
  9. $arr = explode('/', $rootpath);
  10. if(!empty($arr)){
  11. $folder_name = $arr[count($arr)-1];
  12. array_pop($arr);
  13. if(!empty($arr)){
  14. $root_path_upto_folder = implode('/', $arr);
  15. }
  16. }
  17. }
  18.  
  19. if(empty($folder_name) || empty($root_path_upto_folder)){
  20. echo 'Unable to process';
  21. die;
  22. }
  23.  
  24. $the_folder = $root_path_upto_folder.'/'.$folder_name;
  25. $zip_file_name = $folder_name.'.zip';
  26.  
  27.  
  28. $download_file= true;
  29. #$delete_file_after_download = true;
  30.  
  31. class FlxZipArchive extends ZipArchive {
  32.  
  33. public function addDir($location, $name) {
  34. $this->addEmptyDir($name);
  35.  
  36. $this->addDirDo($location, $name);
  37. }
  38.  
  39. private function addDirDo($location, $name) {
  40. $name .= '/';
  41. $location .= '/';
  42.  
  43. $dir = opendir ($location);
  44. while ($file = readdir($dir))
  45. {
  46. if ($file == '.' || $file == '..') continue;
  47. $do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
  48. $this->$do($location . $file, $name . $file);
  49. }
  50. }
  51. }
  52.  
  53. $za = new FlxZipArchive;
  54. $res = $za->open($zip_file_name, ZipArchive::CREATE);
  55. if($res === TRUE)
  56. {
  57. $za->addDir($the_folder, basename($the_folder));
  58. $za->close();
  59. }
  60. else
  61. {
  62. echo 'Could not create a zip archive';
  63. }
  64.  
  65. if ($download_file)
  66. {
  67. ob_get_clean();
  68. header("Pragma: public");
  69. header("Expires: 0");
  70. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  71. header("Cache-Control: private", false);
  72. header("Content-Type: application/zip");
  73. header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";" );
  74. header("Content-Transfer-Encoding: binary");
  75. header("Content-Length: " . filesize($zip_file_name));
  76. readfile($zip_file_name);
  77. }
  78.  
  79. if(file_exists($the_folder.'/'.$zip_file_name)){
  80. unlink($the_folder.'/'.$zip_file_name);
  81. }
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement