Posted By

MMDeveloper on 12/29/09


Tagged

file download force


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

fana
robertstefan
IMPGTX


Force File Download version 2.0 with configurable transfer rates


 / Published in: PHP
 

URL: http://skyward.nefec.org

This is the latest version of my forced download class. This class is part of a much larger system so some slight tailoring will be needed. What I do is pass in an encrypted string which is the filename. The string is decrypted and a filepath appended to it.

It then ensures the filepath/filename is valid. If not, it forces the download of an image with an "error 404" message on it. if its valid, the user it prompted to download the file and you can programmatic-ally control how fast the download goes.

I've also put in a code block where you can override the mime types of certain file extensions if your web server doesn't do it properly for you. Really all you need to worry about is the "performDownload" method because that method accepts the clear-text path to the file to be downloaded.

  1. <?php
  2.  
  3. /**
  4.  * This is the Force File Download class of the Skyward Portal
  5.  * @package Skyward_Landing_Page
  6.  * @subpackage Force File Download
  7.  * @filesource
  8.  * @author Matt Ford
  9.  * @version 1.0
  10.  */
  11.  
  12. class com_forcedownload {
  13.  
  14. /**
  15. * This method executes necessary methods based on request data
  16. * @access public
  17. * @author Matt Ford
  18. * @param array $data configuration and request information
  19. */
  20. public function preProcess($data) {
  21. $data->request["file"] = str_replace(" ", "+", $data->request["file"]);
  22.  
  23. switch ($data->request["task"]) {
  24. case "forcefeed":
  25. default:
  26. $this->forceFeed($data);
  27. break;
  28. }
  29. }
  30.  
  31. /**
  32. * This method determines if the file path is good and executes the necessary method
  33. * @access private
  34. * @author Matt Ford
  35. * @param array $data configuration and request information
  36. */
  37. private function forceFeed ($data) {
  38. global $controller, $controller_config;
  39.  
  40. $file = $controller_config->downloadRootDir . $controller->decrypt($data->request["file"]);
  41.  
  42. if (file_exists($file) === true && is_file($file) === true && is_readable($file) === true) {
  43. $this->performDownload($file);
  44. }
  45. else {
  46. $this->invalidFile($file);
  47. }
  48. }
  49.  
  50. /**
  51. * This method performs the necessary actions to force the users browser to prompt a download box
  52. * @access private
  53. * @author Matt Ford
  54. * @param string $file path to file
  55. */
  56. private function performDownload($file) {
  57. $download_rate = 10000; //kbps
  58. $extension = strtolower(substr(strrchr($file, "."), 1));
  59.  
  60. switch ($extension) {
  61.  
  62. case "doc":
  63. case "docx":
  64. $mime = "application/msword";
  65. break;
  66.  
  67. case "exe":
  68. $mime = "application/octet-stream";
  69. break;
  70.  
  71. case "gif":
  72. $mime = "image/gif";
  73. break;
  74.  
  75. case "jpg":
  76. case "jpeg":
  77. $mime = "image/jpeg";
  78. break;
  79.  
  80. case "txt":
  81. $mime = "text/plain";
  82. break;
  83.  
  84. case "xls":
  85. case "xlsx":
  86. $mime = "application/excel";
  87. break;
  88.  
  89. default:
  90. $mime = "application/octet-stream";
  91. break;
  92. }
  93.  
  94. header ("Content-Description: File Transfer");
  95. header ("Content-Disposition: attachment; filename=\"" . str_replace(" ", "_", basename($file)) . "\";");
  96. header ("Content-Type: " . $mime);
  97. header ("Content-Transfer-Encoding: binary");
  98. header ("Content-Length: " . filesize($file));
  99.  
  100. flush();
  101.  
  102. $fp = fopen($file, "r");
  103.  
  104. while(!feof($fp)) {
  105. print fread($fp, round($download_rate * 1024));
  106. flush();
  107. sleep(1);
  108. }
  109.  
  110. fclose($fp);
  111. die();
  112. }
  113.  
  114. /**
  115. * This method simply echoes an error message of a bad file/path
  116. * @access private
  117. * @author Matt Ford
  118. * @param string $file path to file
  119. */
  120. private function invalidFile($file) {
  121. global $controller_config;
  122. //echo "\"" . basename($file) . "\" is either not a valid file or not in the specified path";
  123. //die();
  124.  
  125. $this->performDownload($controller_config->path . "/supplementalFiles/img/404_File_Not_Found.jpg");
  126. }
  127. }
  128. ?>

Report this snippet  

You need to login to post a comment.