• 技术文章 >php教程 >php手册

    PHP断点续传-HTTP

    2016-06-21 08:52:34原创362
      

      /**

      * PHP-HTTP断点续传实现

      * @param string $path: 文件所在路径

      * @param string $file: 文件名

      * @return void

      */

      function download($path,$file) {

      $real = $path.'/'.$file;

      if(!file_exists($real)) {

      return false;

      }

      $size = filesize($real);

      $size2 = $size-1;

      $range = 0;

      if(isset($_SERVER['HTTP_RANGE'])) {

      header('HTTP /1.1 206 Partial Content');

      $range = str_replace('=','-',$_SERVER['HTTP_RANGE']);

      $range = explode('-',$range);

      $range = trim($range[1]);

      header('Content-Length:'.$size);

      header('Content-Range: bytes '.$range.'-'.$size2.'/'.$size);

      } else {

      header('Content-Length:'.$size);

      header('Content-Range: bytes 0-'.$size2.'/'.$size);

      }

      header('Accenpt-Ranges: bytes');

      header('application/octet-stream');

      header("Cache-control: public");

      header("Pragma: public");

      //解决在IE中下载时中文乱码问题

      $ua = $_SERVER['HTTP_USER_AGENT'];

      if(preg_match('/MSIE/',$ua)) {

      $ie_filename = str_replace('+','%20',urlencode($file));

      header('Content-Dispositon:attachment; filename='.$ie_filename);

      } else {

      header('Content-Dispositon:attachment; filename='.$file);

      }

      $fp = fopen($real,'rb+');

      fseek($fp,$range);

      while(!feof($fp)) {

      set_time_limit(0);

      print(fread($fp,1024));

      flush();

      ob_flush();

      }

      fclose($fp);

      }

      /*End of PHP*/



    php入门到就业线上直播课:查看学习

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:39 header range Content size
    上一篇:PHP分页技术的代码和示例 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• PHP类中的魔术方法(Magic Method)简明总结,magicmethod• mysql 数据备份类代码• 解析用PHP实现var_export的详细介绍• Xgcalendar 新增Php demo• smarty模板引擎从php中获取数据的方法,smarty模板
    1/1

    PHP中文网