php支持分块与断点续传文件下载功能代码

本文章要介绍了这篇文章是一篇关于php流下载,就是可以支持分块与断点续传文件下载,有需要的朋友可以看看,代码如下:

  1. $dowmFile = dirname ( __FILE__ ) . '/Nokia - Always Here.mp3'; //要下载的文件,绝对或相对
  2. $dowmName = 'Nokia - Always Here.mp3';
  3. ob_start ();
  4. getlocalfile ( $dowmFile, $dowmName );
  5. flush ();
  6. ob_flush ();
  7. function getlocalfile($fname, $filename = '') {
  8. $fsize = filesize ( $fname );
  9. header ( 'Cache-Control: public' );
  10. header ( 'Pragma: public' );
  11. header ( 'Accept-Ranges: bytes' );
  12. header ( 'Connection: close' );
  13. header ( 'Content-Type: ' . MIMEType ( $fname ) );
  14. //header('Content-Type: application/octet-stream');
  15. if (isset ( $filename {0} )) {
  16. header ( 'Content-Disposition: attachment;filename=' . $filename );
  17. }
  18. if ($fp = @fopen ( $fname, 'rb' )) {
  19. $start = 0;
  20. $end = $fsize;
  21. $isRange = isset ( $_SERVER ['HTTP_RANGE'] ) && ($_SERVER ['HTTP_RANGE'] != '');
  22. if ($isRange) {
  23. preg_match ( '/^bytes=([0-9]*)-([0-9]*)$/i', $_SERVER ['HTTP_RANGE'], $match );
  24. $start = $match [1];
  25. $end = $match [2];
  26. $isset_start = isset ( $start {0} );
  27. $isset_end = isset ( $end {0} );
  28. if ($isset_start && $isset_end) {
  29. //分块下载
  30. if ($start >= $fsize || $start < 0 || $start > $end) {
  31. $start = 0;
  32. $end = $fsize;
  33. } else if ($end >= $fsize) {
  34. $end = $fsize - $start;
  35. } else {
  36. $end -= $start - 1;
  37. }
  38. } else if ($isset_start && ! $isset_end) {
  39. //指定位置到结束
  40. if ($start >= $fsize || $start < 0) {
  41. $start = 0;
  42. $end = $fsize;
  43. } else {
  44. $end = $fsize - $start;
  45. }
  46. } else if (! $isset_start && $isset_end) {
  47. //最后n个字节
  48. $end = $end > $fsize ? $fsize : $end;
  49. $start = $fsize - $end;
  50. } else {
  51. $start = 0;
  52. $end = $fsize;
  53. }
  54. }
  55. if ($isRange) {
  56. fseek ( $fp, $start );
  57. header ( 'HTTP/1.1 206 Partial Content' );
  58. header ( 'Content-Length: ' . $end );
  59. header ( 'Content-Ranges: bytes ' . $start . '-' . ($end + $start - 1) . '/' . $fsize );
  60. } else {
  61. header ( 'Content-Length: ' . $fsize );
  62. }
  63. if (function_exists ( 'fpassthru' ) && ($end + $start) == $fsize) {
  64. fpassthru ( $fp );
  65. } else {
  66. echo fread ( $fp, $end );
  67. }
  68. } else {
  69. header ( 'Content-Length: ' . $fsize );
  70. readfile ( $fname );
  71. }
  72. //@header("Content-Type: ".mime_content_type($fname));
  73. }
  74. function MIMEType($fname) {
  75. $fileSuffix = strtolower ( substr ( $fname, strrpos ( $fname, '.' ) + 1 ) );
  76. switch ($fileSuffix) {
  77. case 'avi' :
  78. return 'video/msvideo';
  79. case 'wmv' :
  80. return 'video/x-ms-wmv';
  81. case 'txt' :
  82. return 'text/plain';
  83. case 'htm' :
  84. case 'html' :
  85. case 'php' :
  86. return 'text/html';
  87. case 'css' :
  88. return 'text/css';
  89. case 'js' :
  90. return 'application/javascript';
  91. case 'json' :
  92. case 'xml' :
  93. case 'zip' :
  94. case 'pdf' :
  95. case 'rtf' :
  96. case 'tar' :
  97. return 'application/' . $fileSuffix;
  98. case 'swf' :
  99. return 'application/x-shockwave-flash';
  100. case 'flv' :
  101. return 'video/x-flv';
  102. case 'jpe' :
  103. case 'jpg' :
  104. return 'image/jpeg';
  105. case 'jpeg' :
  106. case 'png' :
  107. case 'gif' :
  108. case 'bmp' :
  109. case 'tiff' :
  110. return 'image/' . $fileSuffix;
  111. case 'ico' :
  112. return 'image/vnd.microsoft.icon';
  113. case 'tif' :
  114. return 'image/tiff';
  115. case 'svg' :
  116. case 'svgz' :
  117. return 'image/svg+xml';
  118. case 'rar' :
  119. return 'application/x-rar-compressed';
  120. case 'exe' :
  121. case 'msi' :
  122. return 'application/x-msdownload';
  123. case 'cab' :
  124. return 'application/vnd.ms-cab-compressed';
  125. case 'aif' :
  126. return 'audio/aiff';
  127. case 'mpg' :
  128. case 'mpe' :
  129. case 'mp3' :
  130. return 'audio/mpeg';
  131. case 'mpeg' :
  132. case 'wav' :
  133. case 'aiff' :
  134. return 'audio/' . $fileSuffix;
  135. case 'qt' :
  136. case 'mov' :
  137. return 'video/quicktime';
  138. case 'psd' :
  139. return 'image/vnd.adobe.photoshop';
  140. case 'ai' :
  141. case 'eps' :
  142. case 'ps' :
  143. return 'application/postscript';
  144. case 'doc' :
  145. case 'docx' :
  146. return 'application/msword';
  147. case 'xls' :
  148. case 'xlt' :
  149. case 'xlm' :
  150. case 'xld' :
  151. case 'xla' :
  152. case 'xlc' :
  153. case 'xlw' :
  154. case 'xll' :
  155. return 'application/vnd.ms-excel';
  156. case 'ppt' :
  157. case 'pps' :
  158. return 'application/vnd.ms-powerpoint';
  159. case 'odt' :
  160. return 'application/vnd.oasis.opendocument.text';
  161. case 'ods' :
  162. return 'application/vnd.oasis.opendocument.spreadsheet';
  163. default ://开源代码phpfensi.com
  164. if (function_exists ( 'mime_content_type' )) {
  165. $fileSuffix = mime_content_type ( $filename );
  166. } else {
  167. $fileSuffix = 'application/octet-stream';
  168. }
  169. return $fileSuffix;
  170. break;
  171. }
  172. }