PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/js/ajax/downloads/downloadhandler.php

https://github.com/Tirsofreelance/snippets
PHP | 99 lines | 63 code | 19 blank | 17 comment | 5 complexity | 4da69242337213d9aa0cb26de49f58fe MD5 | raw file
  1. <?php
  2. // get filename
  3. $file = $_GET['file'];
  4. $fileexists = false;
  5. if (strlen($file) > 0) {
  6. // find file location from root
  7. $p = strpos($file, '://');
  8. if ($p > 0) $file = substr($file, strpos($file, '/', $p+3)+1);
  9. // find physical file location
  10. $filepath = str_repeat('../', substr_count($_SERVER["PHP_SELF"], '/')-1).$file;
  11. $filepath = realpath($filepath);
  12. // find filename
  13. $filename = substr(strrchr($file,'/'),1);
  14. // find extension
  15. $ext = strtolower(substr(strrchr($file,'.'),1));
  16. // does file exist
  17. $fileexists = file_exists($filepath);
  18. // determine mime type
  19. switch($ext)
  20. {
  21. case "mp3": $mime="audio/mp3"; break;
  22. case "wav": $mime="audio/x-wav"; break;
  23. case "wma": $mime="audio/x-ms-wma"; break;
  24. case "ogg": $mime="audio/x-ogg"; break;
  25. case "mid": $mime="audio/x-midi"; break;
  26. case "mpg": $mime="video/mpeg"; break;
  27. case "mpeg": $mime="video/mpeg"; break;
  28. case "avi": $mime="video/x-msvideo"; break;
  29. case "wmv": $mime="video/x-ms-wmv"; break;
  30. case "mov": $mime="video/quicktime"; break;
  31. case "qt": $mime="video/quicktime"; break;
  32. case "pdf": $mime="application/pdf"; break;
  33. case "doc": $mime="application/msword"; break;
  34. case "xls": $mime="application/ms-excel"; break;
  35. case "ppt": $mime="application/ms-powerpoint"; break;
  36. case "txt": $mime="text/plain"; break;
  37. case "rtf": $mime="application/rtf"; break;
  38. case "exe": $mime="application/octet-stream"; break;
  39. case "msi": $mime="application/octet-stream"; break;
  40. case "iso": $mime="application/octet-stream"; break;
  41. case "zip": $mime="application/zip"; break;
  42. case "tar": $mime="application/x-tar"; break;
  43. case "gz": $mime="application/x-gzip"; break;
  44. case "rar": $mime="application/rar"; break;
  45. case "lha": $mime="application/lha"; break;
  46. case "jar": $mime="application/x-java-archive"; break;
  47. default: $mime="application/force-download"; break;
  48. }
  49. }
  50. /*
  51. // debug
  52. echo("<p>file: $file</p>");
  53. echo("<p>filepath: $filepath</p>");
  54. echo("<p>filename: $filename</p>");
  55. echo("<p>ext: $ext</p>");
  56. echo("<p>fileexists: $fileexists</p>");
  57. echo("<p>mime: $mime</p>");
  58. */
  59. if ($fileexists) {
  60. if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');
  61. header('Pragma: public');
  62. header('Expires: 0');
  63. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  64. header('Cache-Control: private',false);
  65. header("Content-Type: $mime");
  66. header("Content-Disposition: attachment; filename=\"$filename\"");
  67. header("Content-Transfer-Encoding: binary");
  68. header("Content-Length: ".filesize($filepath));
  69. readfile($filepath);
  70. }
  71. else {
  72. // file not found
  73. header('HTTP/1.0 404 Not Found');
  74. echo('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">');
  75. echo('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" /><title>file not found</title></head><body>');
  76. echo('<h1>file not found</h1><p>Sorry, but the file you requested could not be found.</p><p>Please click back to return to the page you were viewing.</p>');
  77. echo('</body></html>');
  78. }
  79. exit();
  80. ?>