任何文件大小的PHP下载脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了任何文件大小的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. ?>

以上是关于任何文件大小的PHP下载脚本的主要内容,如果未能解决你的问题,请参考以下文章

使用 PHP 下载脚本发送正确的文件大小

文件下载期间文件大小不会显示在 PHP 下载脚本上

我的块大小中没有足够的块 - PHP 下载脚本导致 20% 的文件大小下载

Eclipse 中的通用代码片段或模板

php语法

如何启动使用jQuery自动下载zip文件的PHP脚本