找回密码
 注册加入

扫一扫,极速登录

QQ登录

只需一步,快速开始

搜索
查看: 7513|回复: 0

php流下载(支持分块与断点续传)

[复制链接]
TA的专栏
发表于 2012-12-16 17:07:06 | 显示全部楼层 |阅读模式
该方法通过头信息,指定下载区间和接收下载区间,实现分块下载以及断点续传。
  1. <?php
  2.     $dowmFile = dirname ( __FILE__ ) . '/Nokia - Always Here.mp3'; //要下载的文件,绝对或相对
  3.     $dowmName = 'Nokia - Always Here.mp3';
  4.     ob_start ();
  5.     getlocalfile ( $dowmFile, $dowmName );
  6.     flush ();
  7.     ob_flush ();

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

本版积分规则

Archiver|手机版|小黑屋|Discuz!扩展中心 ( 浙ICP备14042422号-1 )|网站地图QQ机器人

GMT+8, 2024-3-29 01:00 , Processed in 0.397739 second(s), 15 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表