当前位置:首页 > PHP教程 > php应用 > 列表

PHP实现HTTP断点续传的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-28 20:52:32 浏览: 评论:0 

这篇文章主要介绍了PHP实现HTTP断点续传的方法,实例分析了php基于http协议断点续传下载文件的实现方法,需要的朋友可以参考下。

本文实例讲述了PHP实现HTTP断点续传的方法,分享给大家供大家参考,具体实现方法如下:

  1. <?php 
  2. /** 
  3.  * PHP-HTTP断点续传实现 
  4.  * @param string $path: 文件所在路径 
  5.  * @param string $file: 文件名 
  6.  * @return void 
  7.  */ 
  8. function download($path,$file) { 
  9.   $real = $path.'/'.$file; 
  10.   if(!file_exists($real)) { 
  11.     return false; 
  12.   } 
  13.   $size = filesize($real); 
  14.   $size2 = $size-1; 
  15.   $range = 0; 
  16.   if(isset($_SERVER['HTTP_RANGE'])) { 
  17.     header('HTTP /1.1 206 Partial Content'); 
  18.     $range = str_replace('=','-',$_SERVER['HTTP_RANGE']); 
  19.     $range = explode('-',$range); 
  20.     $range = trim($range[1]); 
  21.     header('Content-Length:'.$size); 
  22.     header('Content-Range: bytes '.$range.'-'.$size2.'/'.$size); 
  23.   } else { 
  24.     header('Content-Length:'.$size); 
  25.     header('Content-Range: bytes 0-'.$size2.'/'.$size); 
  26.   } 
  27.   header('Accenpt-Ranges: bytes'); 
  28.   header('application/octet-stream'); 
  29.   header("Cache-control: public"); 
  30.   header("Pragma: public"); 
  31.   //解决在IE中下载时中文乱码问题 
  32.   $ua = $_SERVER['HTTP_USER_AGENT']; 
  33.   if(preg_match('/MSIE/',$ua)) { 
  34.     $ie_filename = str_replace('+','%20',urlencode($file)); 
  35.     header('Content-Dispositon:attachment; filename='.$ie_filename); 
  36.   } else { 
  37.     header('Content-Dispositon:attachment; filename='.$file); 
  38.   } 
  39.   $fp = fopen($real,'rb+'); 
  40.   fseek($fp,$range); 
  41.   while(!feof($fp)) { 
  42.     set_time_limit(0); 
  43.     print(fread($fp,1024)); 
  44.     flush(); 
  45.     ob_flush(); 
  46.   } 
  47.   fclose($fp); 
  48. } 
  49. <?php 
  50. /** 
  51.  * PHP-HTTP断点续传实现 
  52.  * @param string $path: 文件所在路径 
  53.  * @param string $file: 文件名 
  54.  * @return void 
  55.  */ 
  56. function download($path,$file) { 
  57.   $real = $path.'/'.$file; 
  58.   if(!file_exists($real)) { 
  59.     return false; 
  60.   } 
  61.   $size = filesize($real); 
  62.   $size2 = $size-1; 
  63.   $range = 0; 
  64.   if(isset($_SERVER['HTTP_RANGE'])) { 
  65.     header('HTTP /1.1 206 Partial Content'); 
  66.     $range = str_replace('=','-',$_SERVER['HTTP_RANGE']); 
  67.     $range = explode('-',$range); 
  68.     $range = trim($range[1]); 
  69.     header('Content-Length:'.$size); 
  70.     header('Content-Range: bytes '.$range.'-'.$size2.'/'.$size); 
  71.   } else { 
  72.     header('Content-Length:'.$size); 
  73.     header('Content-Range: bytes 0-'.$size2.'/'.$size); 
  74.   } 
  75.   header('Accenpt-Ranges: bytes'); 
  76.   header('application/octet-stream'); 
  77.   header("Cache-control: public"); 
  78.   header("Pragma: public"); 
  79.   //解决在IE中下载时中文乱码问题 
  80.   $ua = $_SERVER['HTTP_USER_AGENT']; 
  81.   if(preg_match('/MSIE/',$ua)) { 
  82.     $ie_filename = str_replace('+','%20',urlencode($file)); 
  83.     header('Content-Dispositon:attachment; filename='.$ie_filename); 
  84.   } else { 
  85.     header('Content-Dispositon:attachment; filename='.$file); 
  86.   } 
  87.   $fp = fopen($real,'rb+'); 
  88.   fseek($fp,$range); 
  89.   while(!feof($fp)) { 
  90.     set_time_limit(0); 
  91.     print(fread($fp,1024)); 
  92.     flush(); 
  93.     ob_flush(); 
  94.   } 
  95.   fclose($fp); 
  96. } 

希望本文所述对大家的php程序设计有所帮助。

Tags: HTTP断点续传

分享到: