Force download function


/ Published in: PHP
Save to your folder(s)

Force download function that worked well, Thanks.


Copy this code and paste it in your HTML
  1. function force_download($filename = '', $data = false, $enable_partial = true, $speedlimit = 0)
  2. {
  3. if ($filename == '')
  4. {
  5. return FALSE;
  6. }
  7.  
  8. if($data === false && !file_exists($filename))
  9. return FALSE;
  10.  
  11. // Try to determine if the filename includes a file extension.
  12. // We need it in order to set the MIME type
  13. if (FALSE === strpos($filename, '.'))
  14. {
  15. return FALSE;
  16. }
  17.  
  18. // Grab the file extension
  19. $x = explode('.', $filename);
  20. $extension = end($x);
  21.  
  22. // Load the mime types
  23. @include(APPPATH.'config/mimes'.EXT);
  24.  
  25. // Set a default mime if we can't find it
  26. if ( ! isset($mimes[$extension]))
  27. {
  28. if (ereg('Opera(/| )([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT']))
  29. $UserBrowser = "Opera";
  30. elseif (ereg('MSIE ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT']))
  31. $UserBrowser = "IE";
  32. else
  33. $UserBrowser = '';
  34.  
  35. $mime = ($UserBrowser == 'IE' || $UserBrowser == 'Opera') ? 'application/octetstream' : 'application/octet-stream';
  36. }
  37. else
  38. {
  39. $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  40. }
  41.  
  42. $size = $data === false ? filesize($filename) : strlen($data);
  43.  
  44. if($data === false)
  45. {
  46. $info = pathinfo($filename);
  47. $name = $info['basename'];
  48. }
  49. else
  50. {
  51. $name = $filename;
  52. }
  53.  
  54. // Clean data in cache if exists
  55.  
  56. // Check for partial download
  57. if(isset($_SERVER['HTTP_RANGE']) && $enable_partial)
  58. {
  59. list($a, $range) = explode("=", $_SERVER['HTTP_RANGE']);
  60. list($fbyte, $lbyte) = explode("-", $range);
  61.  
  62. if(!$lbyte)
  63. $lbyte = $size - 1;
  64.  
  65. $new_length = $lbyte - $fbyte;
  66.  
  67. header("HTTP/1.1 206 Partial Content", true);
  68. header("Content-Length: $new_length", true);
  69. header("Content-Range: bytes $fbyte-$lbyte/$size", true);
  70. }
  71. else
  72. {
  73. header("Content-Length: " . $size);
  74. }
  75.  
  76. // Common headers
  77. header('Content-Type: ' . $mime, true);
  78. header('Content-Disposition: attachment; filename="' . $name . '"', true);
  79. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT", true);
  80. header('Accept-Ranges: bytes', true);
  81. header("Cache-control: private", true);
  82. header('Pragma: private', true);
  83.  
  84. // Open file
  85. if($data === false) {
  86. $file = fopen($filename, 'r');
  87.  
  88. if(!$file)
  89. return FALSE;
  90. }
  91.  
  92. // Cut data for partial download
  93. if(isset($_SERVER['HTTP_RANGE']) && $enable_partial)
  94. if($data === false)
  95. fseek($file, $range);
  96. else
  97. $data = substr($data, $range);
  98.  
  99. // Disable script time limit
  100.  
  101. // Check for speed limit or file optimize
  102. if($speedlimit > 0 || $data === false)
  103. {
  104. if($data === false)
  105. {
  106. $chunksize = $speedlimit > 0 ? $speedlimit * 1024 : 512 * 1024;
  107.  
  108. while(!feof($file) and (connection_status() == 0))
  109. {
  110. $buffer = fread($file, $chunksize);
  111. echo $buffer;
  112. flush();
  113.  
  114. if($speedlimit > 0)
  115. sleep(1);
  116. }
  117.  
  118. fclose($file);
  119. }
  120. else
  121. {
  122. $index = 0;
  123. $speedlimit *= 1024; //convert to kb
  124.  
  125. while($index < $size and (connection_status() == 0))
  126. {
  127. $left = $size - $index;
  128. $buffersize = min($left, $speedlimit);
  129.  
  130. $buffer = substr($data, $index, $buffersize);
  131. $index += $buffersize;
  132.  
  133. echo $buffer;
  134. flush();
  135. sleep(1);
  136. }
  137. }
  138. }
  139. else
  140. {
  141. echo $data;
  142. }
  143. }

URL: http://codeigniter.com/forums/viewthread/71192/#351584

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.