Heim  >  Artikel  >  Backend-Entwicklung  >  php超大文件下载及断点续传下载的实现代码

php超大文件下载及断点续传下载的实现代码

WBOY
WBOYOriginal
2016-07-25 08:59:14861Durchsuche
  1. $sourceFile = "jbxue.tmp"; //要下载的临时文件名
  2. $outFile = "用户订单.xls"; //下载保存到客户端的文件名
  3. $file_extension = strtolower(substr(strrchr($sourceFile, "."), 1)); //获取文件扩展名
  4. //echo $sourceFile;
  5. if (!ereg("[tmp|txt|rar|pdf|doc]", $file_extension))exit ("非法资源下载");
  6. //检测文件是否存在
  7. if (!is_file($sourceFile)) {
  8. die("404 File not found!");
  9. }
  10. $len = filesize($sourceFile); //获取文件大小
  11. $filename = basename($sourceFile); //获取文件名字
  12. $outFile_extension = strtolower(substr(strrchr($outFile, "."), 1)); //获取文件扩展名
  13. //根据扩展名 指出输出浏览器格式
  14. switch ($outFile_extension) {
  15. case "exe" :
  16. $ctype = "application/octet-stream";
  17. break;
  18. case "zip" :
  19. $ctype = "application/zip";
  20. break;
  21. case "mp3" :
  22. $ctype = "audio/mpeg";
  23. break;
  24. case "mpg" :
  25. $ctype = "video/mpeg";
  26. break;
  27. case "avi" :
  28. $ctype = "video/x-msvideo";
  29. break;
  30. default :
  31. $ctype = "application/force-download";
  32. }
  33. //Begin writing headers
  34. header("Cache-Control:");
  35. header("Cache-Control: public");
  36. //设置输出浏览器格式
  37. header("Content-Type: $ctype");
  38. header("Content-Disposition: attachment; filename=" . $outFile);
  39. header("Accept-Ranges: bytes");
  40. $size = filesize($sourceFile);
  41. //如果有$_SERVER['HTTP_RANGE']参数
  42. if (isset ($_SERVER['HTTP_RANGE'])) {
  43. /*Range头域 Range头域可以请求实体的一个或者多个子范围。
  44. 例如,
  45. 表示头500个字节:bytes=0-499
  46. 表示第二个500字节:bytes=500-999
  47. 表示最后500个字节:bytes=-500
  48. 表示500字节以后的范围:bytes=500-
  49. 第一个和最后一个字节:bytes=0-0,-1
  50. 同时指定几个范围:bytes=500-600,601-999
  51. 但是服务器可以忽略此请求头,如果无条件GET包含Range请求头,响应会以状态码206(PartialContent)返回而不是以200 (OK)。
  52. */
  53. // 断点后再次连接 $_SERVER['HTTP_RANGE'] 的值 bytes=4390912-
  54. list ($a, $range) = explode("=", $_SERVER['HTTP_RANGE']);
  55. //if yes, download missing part
  56. str_replace($range, "-", $range); //这句干什么的呢。。。。
  57. $size2 = $size -1; //文件总字节数
  58. $new_length = $size2 - $range; //获取下次下载的长度
  59. header("HTTP/1.1 206 Partial Content");
  60. header("Content-Length: $new_length"); //输入总长
  61. header("Content-Range: bytes $range$size2/$size"); //Content-Range: bytes 4908618-4988927/4988928 95%的时候
  62. } else {
  63. //第一次连接
  64. $size2 = $size -1;
  65. header("Content-Range: bytes 0-$size2/$size"); //Content-Range: bytes 0-4988927/4988928
  66. header("Content-Length: " . $size); //输出总长
  67. }
  68. //打开文件
  69. //edit bbs.it-home.org
  70. $fp = fopen("$sourceFile", "rb");
  71. //设置指针位置
  72. fseek($fp, $range);
  73. //虚幻输出
  74. while (!feof($fp)) {
  75. //设置文件最长执行时间
  76. set_time_limit(0);
  77. print (fread($fp, 1024 * 8)); //输出文件
  78. flush(); //输出缓冲
  79. ob_flush();
  80. }
  81. fclose($fp);
  82. exit ();
  83. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn