服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > php教程 >

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

时间:2016-05-11 12:42来源: 作者: 点击:
本文章要介绍了这篇文章是一篇关于php流下载,就是可以支持分块与断点续传文件下载,有需要的朋友可以看看,代码如下: $dowmFile =dirname( __FILE__ ). /Nokia-AlwaysHere.mp3 ; //要下载的文件,绝对

本文章要介绍了这篇文章是一篇关于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. function MIMEType($fname) { 
  74.   $fileSuffix = strtolower ( substr ( $fnamestrrpos ( $fname'.' ) + 1 ) ); 
  75.   switch ($fileSuffix) { 
  76.     case 'avi' : 
  77.       return 'video/msvideo'
  78.     case 'wmv' : 
  79.       return 'video/x-ms-wmv'
  80.     case 'txt' : 
  81.       return 'text/plain'
  82.     case 'htm' : 
  83.     case 'html' : 
  84.     case 'php' : 
  85.       return 'text/html'
  86.     case 'css' : 
  87.       return 'text/css'
  88.     case 'js' : 
  89.       return 'application/javascript'
  90.     case 'json' : 
  91.     case 'xml' : 
  92.     case 'zip' : 
  93.     case 'pdf' : 
  94.     case 'rtf' : 
  95.     case 'tar' : 
  96.       return 'application/' . $fileSuffix
  97.     case 'swf' : 
  98.       return 'application/x-shockwave-flash'
  99.     case 'flv' : 
  100.       return 'video/x-flv'
  101.     case 'jpe' : 
  102.     case 'jpg' : 
  103.       return 'image/jpeg'
  104.     case 'jpeg' : 
  105.     case 'png' : 
  106.     case 'gif' : 
  107.     case 'bmp' : 
  108.     case 'tiff' : 
  109.       return 'image/' . $fileSuffix
  110.     case 'ico' : 
  111.       return 'image/vnd.microsoft.icon'
  112.     case 'tif' : 
  113.       return 'image/tiff'
  114.     case 'svg' : 
  115.     case 'svgz' : 
  116.       return 'image/svg+xml'
  117.     case 'rar' : 
  118.       return 'application/x-rar-compressed'
  119.     case 'exe' : 
  120.     case 'msi' : 
  121.       return 'application/x-msdownload'
  122.     case 'cab' : 
  123.       return 'application/vnd.ms-cab-compressed'
  124.     case 'aif' : 
  125.       return 'audio/aiff'
  126.     case 'mpg' : 
  127.     case 'mpe' : 
  128.     case 'mp3' : 
  129.       return 'audio/mpeg'
  130.     case 'mpeg' : 
  131.     case 'wav' : 
  132.     case 'aiff' : 
  133.       return 'audio/' . $fileSuffix
  134.     case 'qt' : 
  135.     case 'mov' : 
  136.       return 'video/quicktime'
  137.     case 'psd' : 
  138.       return 'image/vnd.adobe.photoshop'
  139.     case 'ai' : 
  140.     case 'eps' : 
  141.     case 'ps' : 
  142.       return 'application/postscript'
  143.     case 'doc' : 
  144.     case 'docx' : 
  145.       return 'application/msword'
  146.     case 'xls' : 
  147.     case 'xlt' : 
  148.     case 'xlm' : 
  149.     case 'xld' : 
  150.     case 'xla' : 
  151.     case 'xlc' : 
  152.     case 'xlw' : 
  153.     case 'xll' : 
  154.       return 'application/vnd.ms-excel'
  155.     case 'ppt' : 
  156.     case 'pps' : 
  157.       return 'application/vnd.ms-powerpoint'
  158.     case 'odt' : 
  159.       return 'application/vnd.oasis.opendocument.text'
  160.     case 'ods' : 
  161.       return 'application/vnd.oasis.opendocument.spreadsheet'
  162.     default ://开源代码最模板zuimoban.com 
  163.       if (function_exists ( 'mime_content_type' )) { 
  164.         $fileSuffix = mime_content_type ( $filename ); 
  165.       } else { 
  166.         $fileSuffix = 'application/octet-stream'
  167.       } 
  168.       return $fileSuffix
  169.       break
  170.   } 
  171.  
(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容