download.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. $zip_dir = elgg_get_config("dataroot") . 'file_tools/zip_temp/';
  3. if (!file_exists($zip_dir)) {
  4. mkdir($zip_dir, 0755, true);
  5. }
  6. $zip_filename = tempnam($zip_dir, "download_");
  7. $file_guids = get_input("file_guids");
  8. $folder_guids = get_input("folder_guids");
  9. if (empty($file_guids) && empty($folder_guids)) {
  10. register_error(elgg_echo("InvalidParameterException:MissingParameter"));
  11. forward(REFERER);
  12. }
  13. $zip = new ZipArchive();
  14. if ($zip->open($zip_filename, ZIPARCHIVE::CREATE) !== true) {
  15. register_error(elgg_echo("file:downloadfailed"));
  16. forward(REFERER);
  17. }
  18. // add files to the zip
  19. if (!empty($file_guids)) {
  20. foreach ($file_guids as $file_guid) {
  21. if ($file = get_entity($file_guid)) {
  22. // check if the name exists in the zip
  23. if ($zip->statName($file->originalfilename) === false) {
  24. // doesn't exist, so add
  25. $zip->addFile($file->getFilenameOnFilestore(), $file->originalfilename);
  26. } else {
  27. // file name exists, so create a new one
  28. $ext_pos = strrpos($file->originalfilename, ".");
  29. $file_name = substr($file->originalfilename, 0, $ext_pos) . "_" . $file->getGUID() . substr($file->originalfilename, $ext_pos);
  30. $zip->addFile($file->getFilenameOnFilestore(), $file_name);
  31. }
  32. }
  33. }
  34. }
  35. // add folder (and their content) to the zip
  36. if (!empty($folder_guids)) {
  37. foreach ($folder_guids as $folder_guid) {
  38. if ($folder = get_entity($folder_guid)) {
  39. file_tools_add_folder_to_zip($zip, $folder);
  40. }
  41. }
  42. }
  43. // done adding content, so save the zip
  44. $zip->close();
  45. if (file_exists($zip_filename)) {
  46. // output the correct headers
  47. header('Pragma: public');
  48. header('Content-type: application/zip');
  49. header('Content-Disposition: attachment; filename="folder_contents.zip"');
  50. header('Content-Length: ' . filesize($zip_filename));
  51. ob_clean();
  52. flush();
  53. readfile($zip_filename);
  54. unlink($zip_filename);
  55. } else {
  56. register_error(elgg_echo("file:downloadfailed"));
  57. forward(REFERER);
  58. }