Posted By

deanhouseholder on 09/12/13


Tagged

php filesize download apache limit


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

IMPGTX
Priestd09


PHP Download script for any filesize


 / Published in: PHP
 

By default Apache won't allow you to download a file over 2GB. This PHP script overcomes that along with some extra goodies.

The script will read from standard url path the file on the server to download and display the filesize along with a link to download it.

Setup:

1) Create a directory called "d" on the root of your website and name this script "index.php"

2) Create a .htaccess file with the following lines:

RewriteEngine On

RewriteBase /d/

RewriteCond %{REQUEST_DIRECTORY} !-d [NC]

RewriteRule ^(.*)$ index.php?f=$1 [L]

3) Craft a url in the format of: http://[website_url]/d/path/to/download/filename.dat

This will link to a file: http://[website_url]/path/to/download/filename.dat

If you want, you can also craft a url with a trailing "&" and it will automatically start downloading.

Example: http://[website_url]/path/to/download/filename.dat&

  1. <?php
  2.  
  3. // Use PHP to serve files too big for Apache to deliver (>2GB)
  4. function serveFile($file){
  5. if (file_exists($file)){
  6. $path_parts = pathinfo($file);
  7. $as = $path_parts['basename'];
  8. // Exec to determine correct file size (see's bigger files than PHP can see)
  9. $size = trim(`stat -c%s "$file"`);
  10.  
  11. header("Expires: Mon, 1 Jan 2010 01:00:00 GMT");
  12. header("Pragma: no-cache");
  13. header("Cache-Control: private");
  14. header("Content-Description: File Download");
  15. header("Content-Type: application/octet-stream");
  16. header("Content-Disposition: attachment; filename=\"".urlencode($as)."\"");
  17. header("Content-Length: $size");
  18. header("Content-Transfer-Encoding: binary");
  19.  
  20. flush();
  21. $fp = popen("cat \"$file\" 2>&1", "r");
  22. while(!feof($fp))
  23. {
  24. // send the current file part to the browser
  25. print fread($fp, 1024);
  26. // flush the content to the browser
  27. flush();
  28. }
  29. fclose($fp);
  30. } else {
  31. header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
  32. print "File not found";
  33. }
  34. }
  35.  
  36. $file = "../" . str_replace(array("/d/", "?f=","../"), "", trim(urldecode($_SERVER['REQUEST_URI'])));
  37.  
  38. // If the last character of the URL is & then trigger for Download
  39. if (substr($_SERVER['REQUEST_URI'], -1) == "&"){
  40. $download = true;
  41. // Strip off & from the end of the $file variable
  42. $file = substr_replace($file, '', strlen($file)-1, 1);
  43. }else{
  44. $download = false;
  45. }
  46.  
  47. if (isset($_GET['f']) && $download == false){
  48. if (file_exists($file)){
  49. $size = number_format(trim(`stat -c%s "$file"`));
  50. $path_parts = pathinfo($file);
  51. $url_path = str_replace(array(" ","../"), array("%20",""), $file);
  52. print "Click to download <a href=\"/d/?f=".$url_path."&\">".$path_parts['basename']."</a>";
  53. print " (".$size." bytes)";
  54. } else {
  55. header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
  56. print "File not found";
  57. }
  58. } else {
  59. serveFile($file);
  60. }
  61.  
  62. ?>

Report this snippet  

You need to login to post a comment.