当前位置:首页 > PHP教程 > php上传下载 > 列表

PHP如何实现断点续传大文件?

发布:smiling 来源: PHP粉丝网  添加日期:2022-07-02 09:40:05 浏览: 评论:0 

一、断点续传原理

所谓断点续传,也就是要从文件已经下载的地方开始继续下载。在以前版本的 HTTP 协议是不支持断点的,HTTP/1.1 开始就支持了。一般断点下载时才用到 Range 和 Content-Range 实体头。

不使用断点续传

get /down.zip http/1.1<br data-filtered="filtered">accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-<br data-filtered="filtered">excel, application/msword, application/vnd.ms-powerpoint, */*<br data-filtered="filtered">accept-language: zh-cn<br data-filtered="filtered">accept-encoding: gzip, deflate<br data-filtered="filtered">user-agent: mozilla/4.0 (compatible; msie 5.01; windows nt 5.0)<br data-filtered="filtered">connection: keep-alive<br data-filtered="filtered">

服务器收到请求后,按要求寻找请求的文件,提取文件的信息,然后返回给浏览器,返回信息如下:

HTTP/1.1 200 Ok<br data-filtered="filtered">content-length=106786028<br data-filtered="filtered">accept-ranges=bytes<br data-filtered="filtered">date=mon, 30 apr 2001 12:56:11 gmt<br data-filtered="filtered">etag=w/"02ca57e173c11:95b"<br data-filtered="filtered">content-type=application/octet-stream<br data-filtered="filtered">server=microsoft-iis/5.0<br data-filtered="filtered">last-modified=mon, 30 apr 2001 12:56:11 gmt<br data-filtered="filtered">

使用断点续传

GET /down.zip HTTP/1.0<br data-filtered="filtered">User-Agent: NetFox<br data-filtered="filtered">RANGE: bytes=2000070-<br data-filtered="filtered">Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2<br data-filtered="filtered">

多了这么一行Range: bytes=2000070-

这一行的意思就是告诉服务器down.zip这个文件从2000070字节开始传,前面的字节不用传了。

Range的完整格式是:

Range: bytes=startOffset-targetOffset/sum [表示从startOffset读取,一直读取到targetOffset位置,读取总数为sum直接]<br data-filtered="filtered"> <br data-filtered="filtered">Range: bytes=startOffset-targetOffset [字节总数也可以去掉]<br data-filtered="filtered">

服务器收到这个请求以后,返回的信息如下:

HTTP/1.1 206 Partial Content<br data-filtered="filtered">content-length=106786028<br data-filtered="filtered">content-range=bytes 2000070-106786027/106786028<br data-filtered="filtered">date=mon, 30 apr 2001 12:55:20 gmt<br data-filtered="filtered">etag=w/"02ca57e173c11:95b"<br data-filtered="filtered">content-type=application/octet-stream<br data-filtered="filtered">server=microsoft-iis/5.0<br data-filtered="filtered">last-modified=mon, 30 apr 2001 12:55:20 gmt<br data-filtered="filtered">

和前面服务器返回的信息比较一下,就会发现增加了一行:

Content-Range=bytes 2000070-106786027/106786028<br data-filtered="filtered">

返回的代码也改为206了,而不再是200了。

HTTP/1.1 206 Partial Content<br data-filtered="filtered">

知道了以上原理,就可以进行断点续传的编程了。

二、PHP实现

  1. /** php下载类,支持断点续传<br data-filtered="filtered"> * download: 下载文件<br data-filtered="filtered"> * setSpeed: 设置下载速度<br data-filtered="filtered"> * getRange: 获取header中Range<br data-filtered="filtered"> */<br data-filtered="filtered"> <br data-filtered="filtered">class FileDownload{<br data-filtered="filtered"> <br data-filtered="filtered"/** 下载<br data-filtered="filtered"> * @param String $file 要下载的文件路径<br data-filtered="filtered"> * @param String $name 文件名称,为空则与下载的文件名称一样<br data-filtered="filtered"> * @param boolean $reload 是否开启断点续传<br data-filtered="filtered"> */<br data-filtered="filtered"public function download($file$name=''$reload=false){<br data-filtered="filtered"$fp = @fopen($file'rb');<br data-filtered="filtered"if($fp){<br data-filtered="filtered"if($name==''){<br data-filtered="filtered"$name = basename($file);<br data-filtered="filtered"> }<br data-filtered="filtered"$header_array = get_headers($file, true);<br data-filtered="filtered"//var_dump($header_array);die;<br data-filtered="filtered"> // 下载本地文件,获取文件大小<br data-filtered="filtered"> if (!$header_array) {<br data-filtered="filtered"> $file_size = filesize($file);<br data-filtered="filtered"> } else {<br data-filtered="filtered"> $file_size = $header_array['Content-Length'];<br data-filtered="filtered"> }<br data-filtered="filtered"> $ranges = $this->getRange($file_size);<br data-filtered="filtered"> $ua = $_SERVER["HTTP_USER_AGENT"];//判断是什么类型浏览器<br data-filtered="filtered"> header('cache-control:public');<br data-filtered="filtered"> header('content-type:application/octet-stream'); <br data-filtered="filtered"> <br data-filtered="filtered"> $encoded_filename = urlencode($name);<br data-filtered="filtered"> $encoded_filename = str_replace("+", "%20", $encoded_filename);<br data-filtered="filtered"> <br data-filtered="filtered"> //解决下载文件名乱码<br data-filtered="filtered"> if (preg_match("/MSIE/", $ua) || preg_match("/Trident/", $ua) ){ <br data-filtered="filtered"> header('Content-Disposition: attachment; filename="' .$encoded_filename . '"');<br data-filtered="filtered"> } else if (preg_match("/Firefox/", $ua)) {<br data-filtered="filtered"> header('Content-Disposition: attachment; filename*="utf8\'\'' . $name . '"');<br data-filtered="filtered"> }else if (preg_match("/Chrome/", $ua)) {<br data-filtered="filtered"> header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');<br data-filtered="filtered"> } else {<br data-filtered="filtered"> header('Content-Disposition: attachment; filename="' . $name . '"');<br data-filtered="filtered"> }<br data-filtered="filtered"> //header('Content-Disposition: attachment; filename="' . $name . '"');<br data-filtered="filtered"> <br data-filtered="filtered"> if($reload && $ranges!=null){ // 使用续传<br data-filtered="filtered"> header('HTTP/1.1 206 Partial Content');<br data-filtered="filtered"> header('Accept-Ranges:bytes');<br data-filtered="filtered"> <br data-filtered="filtered"> // 剩余长度<br data-filtered="filtered"> header(sprintf('content-length:%u',$ranges['end']-$ranges['start']));<br data-filtered="filtered"> <br data-filtered="filtered"> // range信息<br data-filtered="filtered"> header(sprintf('content-range:bytes %s-%s/%s', $ranges['start'], $ranges['end'], $file_size));<br data-filtered="filtered"> //file_put_contents('test.log',sprintf('content-length:%u',$ranges['end']-$ranges['start']),FILE_APPEND);<br data-filtered="filtered"> // fp指针跳到断点位置<br data-filtered="filtered"> fseek($fp, sprintf('%u', $ranges['start']));<br data-filtered="filtered"> }else{<br data-filtered="filtered"> file_put_contents('test.log','2222',FILE_APPEND);<br data-filtered="filtered"> header('HTTP/1.1 200 OK');<br data-filtered="filtered"> header('content-length:'.$file_size);<br data-filtered="filtered"> }<br data-filtered="filtered"> <br data-filtered="filtered"> while(!feof($fp)){<br data-filtered="filtered"> //echo fread($fp, round($this->_speed*1024,0));<br data-filtered="filtered"> //echo fread($fp, $file_size);<br data-filtered="filtered"> echo fread($fp, 4096);<br data-filtered="filtered"> ob_flush();<br data-filtered="filtered"> }<br data-filtered="filtered"> <br data-filtered="filtered"> ($fp!=null) && fclose($fp);<br data-filtered="filtered"> }else{<br data-filtered="filtered"> return '';<br data-filtered="filtered"> }<br data-filtered="filtered"> }<br data-filtered="filtered"> <br data-filtered="filtered"> /** 设置下载速度<br data-filtered="filtered"> * @param int $speed<br data-filtered="filtered"> */<br data-filtered="filtered"> public function setSpeed($speed){<br data-filtered="filtered"> if(is_numeric($speed) && $speed>16 && $speed<4096){<br data-filtered="filtered"> $this->_speed = $speed;<br data-filtered="filtered"> }<br data-filtered="filtered"> }<br data-filtered="filtered"> <br data-filtered="filtered"> /** 获取header range信息<br data-filtered="filtered"> * @param int $file_size 文件大小<br data-filtered="filtered"> * @return Array<br data-filtered="filtered"> */<br data-filtered="filtered"> private function getRange($file_size){<br data-filtered="filtered"> //file_put_contents('range.log', json_encode($_SERVER), FILE_APPEND);<br data-filtered="filtered"> if(isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE'])){<br data-filtered="filtered"> $range = $_SERVER['HTTP_RANGE'];<br data-filtered="filtered"> $range = preg_replace('/[\s|,].*/', '', $range);<br data-filtered="filtered"> $range = explode('-', substr($range, 6));<br data-filtered="filtered"> if(count($range)<2){<br data-filtered="filtered"> $range[1] = $file_size;<br data-filtered="filtered"> }<br data-filtered="filtered"> $range = array_combine(array('start','end'), $range);<br data-filtered="filtered"> if(empty($range['start'])){<br data-filtered="filtered"> $range['start'] = 0;<br data-filtered="filtered"> }<br data-filtered="filtered"> if(empty($range['end'])){<br data-filtered="filtered"> $range['end'] = $file_size;<br data-filtered="filtered"> }<br data-filtered="filtered"> return $range;<br data-filtered="filtered"> }<br data-filtered="filtered"> return null;<br data-filtered="filtered"> }<br data-filtered="filtered">}<br data-filtered="filtered"> <br data-filtered="filtered">$obj = new FileDownload();<br data-filtered="filtered">$obj->download('http://down.golaravel.com/laravel/laravel-master.zip','', true);<br data-filtered="filtered">

Tags: PHP断点续传大文件

分享到: