• 沙里软件

  • ShaliSoft.com [手机站]   办公桌收纳抽屉
  • 首页
  • 博文
  • 演示
  • 管理
  • php支持分块与断点续传文件下载功能代码

    网络   2013/10/2 21:27:27
    $dowmFile = dirname ( __FILE__ ) . '/Nokia - Always Here.mp3'; //要下载的文件,绝对或相对
    $dowmName = 'Nokia - Always Here.mp3';
    ob_start ();
    getlocalfile ( $dowmFile, $dowmName );
    flush ();
    ob_flush ();
    function getlocalfile($fname, $filename = '') {
      $fsize = filesize ( $fname );
      header ( 'Cache-Control: public' );
      header ( 'Pragma: public' );
      header ( 'Accept-Ranges: bytes' );
      header ( 'Connection: close' );
      header ( 'Content-Type: ' . MIMEType ( $fname ) );
      //header('Content-Type: application/octet-stream');
      if (isset ( $filename {0} )) {
        header ( 'Content-Disposition: attachment;filename=' . $filename );
      }
      if ($fp = @fopen ( $fname, 'rb' )) {
        $start = 0;
        $end = $fsize;
        $isRange = isset ( $_SERVER ['HTTP_RANGE'] ) && ($_SERVER ['HTTP_RANGE'] != '');
        if ($isRange) {
          preg_match ( '/^bytes=([0-9]*)-([0-9]*)$/i', $_SERVER ['HTTP_RANGE'], $match );
          $start = $match [1];
          $end = $match [2];
          $isset_start = isset ( $start {0} );
          $isset_end = isset ( $end {0} );
          if ($isset_start && $isset_end) {
            //分块下载
            if ($start >= $fsize || $start < 0 || $start > $end) {
              $start = 0;
              $end = $fsize;
            } else if ($end >= $fsize) {
              $end = $fsize - $start;
            } else {
              $end -= $start - 1;
            }
          } else if ($isset_start && ! $isset_end) {
            //指定位置到结束
            if ($start >= $fsize || $start < 0) {
              $start = 0;
              $end = $fsize;
            } else {
              $end = $fsize - $start;
            }
          } else if (! $isset_start && $isset_end) {
            //最后n个字节
            $end = $end > $fsize ? $fsize : $end;
            $start = $fsize - $end;
          } else {
            $start = 0;
            $end = $fsize;
          }
        }
        if ($isRange) {
          fseek ( $fp, $start );
          header ( 'HTTP/1.1 206 Partial Content' );
          header ( 'Content-Length: ' . $end );
          header ( 'Content-Ranges: bytes ' . $start . '-' . ($end + $start - 1) . '/' . $fsize );
        } else {
          header ( 'Content-Length: ' . $fsize );
        }
        if (function_exists ( 'fpassthru' ) && ($end + $start) == $fsize) {
          fpassthru ( $fp );
        } else {
          echo fread ( $fp, $end );
        }
      } else {
        header ( 'Content-Length: ' . $fsize );
        readfile ( $fname );
      }
      //@header("Content-Type: ".mime_content_type($fname));
    }
    function MIMEType($fname) {
      $fileSuffix = strtolower ( substr ( $fname, strrpos ( $fname, '.' ) + 1 ) );
      switch ($fileSuffix) {
        case 'avi' :
          return 'video/msvideo';
        case 'wmv' :
          return 'video/x-ms-wmv';
        case 'txt' :
          return 'text/plain';
        case 'htm' :
        case 'html' :
        case 'php' :
          return 'text/html';
        case 'css' :
          return 'text/css';
        case 'js' :
          return 'application/javascript';
        case 'json' :
        case 'xml' :
        case 'zip' :
        case 'pdf' :
        case 'rtf' :
        case 'tar' :
          return 'application/' . $fileSuffix;
        case 'swf' :
          return 'application/x-shockwave-flash';
        case 'flv' :
          return 'video/x-flv';
        case 'jpe' :
        case 'jpg' :
          return 'image/jpeg';
        case 'jpeg' :
        case 'png' :
        case 'gif' :
        case 'bmp' :
        case 'tiff' :
          return 'image/' . $fileSuffix;
        case 'ico' :
          return 'image/vnd.microsoft.icon';
        case 'tif' :
          return 'image/tiff';
        case 'svg' :
        case 'svgz' :
          return 'image/svg+xml';
        case 'rar' :
          return 'application/x-rar-compressed';
        case 'exe' :
        case 'msi' :
          return 'application/x-msdownload';
        case 'cab' :
          return 'application/vnd.ms-cab-compressed';
        case 'aif' :
          return 'audio/aiff';
        case 'mpg' :
        case 'mpe' :
        case 'mp3' :
          return 'audio/mpeg';
        case 'mpeg' :
        case 'wav' :
        case 'aiff' :
          return 'audio/' . $fileSuffix;
        case 'qt' :
        case 'mov' :
          return 'video/quicktime';
        case 'psd' :
          return 'image/vnd.adobe.photoshop';
        case 'ai' :
        case 'eps' :
        case 'ps' :
          return 'application/postscript';
        case 'doc' :
        case 'docx' :
          return 'application/msword';
        case 'xls' :
        case 'xlt' :
        case 'xlm' :
        case 'xld' :
        case 'xla' :
        case 'xlc' :
        case 'xlw' :
        case 'xll' :
          return 'application/vnd.ms-excel';
        case 'ppt' :
        case 'pps' :
          return 'application/vnd.ms-powerpoint';
        case 'odt' :
          return 'application/vnd.oasis.opendocument.text';
        case 'ods' :
          return 'application/vnd.oasis.opendocument.spreadsheet';
        default :
          if (function_exists ( 'mime_content_type' )) {
            $fileSuffix = mime_content_type ( $filename );
          } else {
            $fileSuffix = 'application/octet-stream';
          }
          return $fileSuffix;
          break;
      }
    }


    阅读(4583) 分享(0)

    上一篇: PHP异步调用socket简单实例
    下一篇: php利用json数据实现跨域操作实例

  • 精彩推荐

    ◆ 导出Excel 格式 mso-number-format
    ◆ JS 银行卡号判断验证
    ◆ StackOverflow网站架构,.NET技术架构解析
    ◆ 移动设备屏幕适配,字体px pt dp的区别?
    ◆ 怎么防止数据库被下载密码不被泄露
    ◆ 58同城沈剑:好的架构是进化来的,不是设计来的
    ◆ 关闭vs2010的托盘提示 "Improve Microsoft Visual Studio performance"
    ◆ CIH病毒作者陈盈豪现状
    ◆ PHP利用mysqli操作数据库方法总结
    ◆ 马云、马化腾、刘强东的童年大揭底!
  • 用心做事 不能唯利是图

    • 吊儿
    • 用QQ联系我17905772
  • 搜索


  • 最新文章

    • 导出Excel 格式 mso-number-format
    • 服务器iis支持tls1.2,windows server 2008 r2 中IIS启用TLS 1.2(安装SSL后用TLS 1.2)
    • MySQL配置优化
    • EditPlus 添加文件比较工具winmerge
    • 滚动悬浮固定JS特效

  • 热门文章

    • php sso单点登录实现代码
    • 中国菜刀(China chopper) 最新黑客工具
    • redis.conf中文版(基于2.4)
    • 搜索引擎名单大全
    • php图片上传类,支持加水印,生成略缩图

  • 最新图库


  • 最新评论


  • 友情链接

  • 沙里软件

  • 最近访客

    Powered by ShaliSoft.com 豫ICP备13008529号

    免责声明:本站部分内容来源于互联网,转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,不为其版权负责,也不构成任何其他建议。如果发现侵犯版权,联系QQ17905772进行删除。