Advertisement
Pythorian

Untitled

Mar 19th, 2014
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. <?php
  2.  
  3. $zipname = 'bk.zip';
  4. function folderToZip($folder, &$zipFile, $exclusiveLength)
  5. {
  6. set_time_limit(0);
  7. $handle = opendir($folder);
  8. while (false !== $f = readdir($handle))
  9. {
  10. if ($f != '.' && $f != '..')
  11. {
  12. $filePath = "$folder/$f";
  13. $localPath = substr($filePath, $exclusiveLength);
  14. if (is_file($filePath))
  15. {
  16. $zipFile->addFile($filePath, $localPath);
  17. }
  18. elseif (is_dir($filePath))
  19. {
  20. $zipFile->addEmptyDir($localPath);
  21. folderToZip($filePath, $zipFile, $exclusiveLength);
  22. }
  23. }
  24. }
  25. closedir($handle);
  26. }
  27.  
  28. function zipDir($sourcePath, $outZipPath)
  29. {
  30. $pathInfo = pathInfo($sourcePath);
  31. $parentPath = $pathInfo['dirname'];
  32. $dirName = $pathInfo['basename'];
  33.  
  34. $z = new ZipArchive();
  35. $z->open($outZipPath, ZIPARCHIVE::CREATE);
  36. $z->addEmptyDir($dirName);
  37. folderToZip($sourcePath, $z, strlen("$parentPath/"));
  38. $z->close();
  39. }
  40. zipDir('.', $zipname);
  41.  
  42. header('Content-Type: application/zip');
  43. header("Content-Disposition: attachment; filename='bk.zip'");
  44. header('Content-Length: ' . filesize($zipname));
  45. header("Location: bk.zip");
  46.  
  47. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement