一、斷點續傳原理
所謂斷點續傳,也就是要從檔案已經下載的地方開始繼續下載。在先前版本的 HTTP 協定是不支援斷點的,HTTP/1.1 開始就支援了。一般斷點下載時才用到 Range 和 Content-Range 實體頭。
不使用斷點續傳
get /down.zip http/1.1<br/>accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-<br/>excel, application/msword, application/vnd.ms-powerpoint, */*<br/>accept-language: zh-cn<br/>accept-encoding: gzip, deflate<br/>user-agent: mozilla/4.0 (compatible; msie 5.01; windows nt 5.0)<br/>connection: keep-alive<br/>
伺服器收到請求後,按要求尋找請求的文件,提取文件的信息,然後返回給瀏覽器,返回信息如下:
HTTP/1.1 200 Ok<br/>content-length=106786028<br/>accept-ranges=bytes<br/>date=mon, 30 apr 2001 12:56:11 gmt<br/>etag=w/"02ca57e173c11:95b"<br/>content-type=application/octet-stream<br/>server=microsoft-iis/5.0<br/>last-modified=mon, 30 apr 2001 12:56:11 gmt<br/>
使用斷點續傳
GET /down.zip HTTP/1.0<br/>User-Agent: NetFox<br/>RANGE: bytes=2000070-<br/>Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2<br/>
多了這麼一行Range: bytes=2000070-<br/>
Range的完整格式是:
Range: bytes=startOffset-targetOffset/sum [表示从startOffset读取,一直读取到targetOffset位置,读取总数为sum直接]<br/> <br/>Range: bytes=startOffset-targetOffset [字节总数也可以去掉]<br/>
HTTP/1.1 206 Partial Content<br/>content-length=106786028<br/>content-range=bytes 2000070-106786027/106786028<br/>date=mon, 30 apr 2001 12:55:20 gmt<br/>etag=w/"02ca57e173c11:95b"<br/>content-type=application/octet-stream<br/>server=microsoft-iis/5.0<br/>last-modified=mon, 30 apr 2001 12:55:20 gmt<br/>
Content-Range=bytes 2000070-106786027/106786028<br/>
HTTP/1.1 206 Partial Content<br/>
/** php下载类,支持断点续传<br/> * download: 下载文件<br/> * setSpeed: 设置下载速度<br/> * getRange: 获取header中Range<br/> */<br/> <br/>class FileDownload{<br/> <br/> /** 下载<br/> * @param String $file 要下载的文件路径<br/> * @param String $name 文件名称,为空则与下载的文件名称一样<br/> * @param boolean $reload 是否开启断点续传<br/> */<br/> public function download($file, $name='', $reload=false){<br/> $fp = @fopen($file, 'rb');<br/> if($fp){<br/> if($name==''){<br/> $name = basename($file);<br/> }<br/> $header_array = get_headers($file, true);<br/> //var_dump($header_array);die;<br/> // 下载本地文件,获取文件大小<br/> if (!$header_array) {<br/> $file_size = filesize($file);<br/> } else {<br/> $file_size = $header_array['Content-Length'];<br/> }<br/> $ranges = $this->getRange($file_size);<br/> $ua = $_SERVER["HTTP_USER_AGENT"];//判断是什么类型浏览器<br/> header('cache-control:public');<br/> header('content-type:application/octet-stream'); <br/> <br/> $encoded_filename = urlencode($name);<br/> $encoded_filename = str_replace("+", "%20", $encoded_filename);<br/> <br/> //解决下载文件名乱码<br/> if (preg_match("/MSIE/", $ua) || preg_match("/Trident/", $ua) ){ <br/> header('Content-Disposition: attachment; filename="' .$encoded_filename . '"');<br/> } else if (preg_match("/Firefox/", $ua)) {<br/> header('Content-Disposition: attachment; filename*="utf8\'\'' . $name . '"');<br/> }else if (preg_match("/Chrome/", $ua)) {<br/> header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');<br/> } else {<br/> header('Content-Disposition: attachment; filename="' . $name . '"');<br/> }<br/> //header('Content-Disposition: attachment; filename="' . $name . '"');<br/> <br/> if($reload && $ranges!=null){ // 使用续传<br/> header('HTTP/1.1 206 Partial Content');<br/> header('Accept-Ranges:bytes');<br/> <br/> // 剩余长度<br/> header(sprintf('content-length:%u',$ranges['end']-$ranges['start']));<br/> <br/> // range信息<br/> header(sprintf('content-range:bytes %s-%s/%s', $ranges['start'], $ranges['end'], $file_size));<br/> //file_put_contents('test.log',sprintf('content-length:%u',$ranges['end']-$ranges['start']),FILE_APPEND);<br/> // fp指针跳到断点位置<br/> fseek($fp, sprintf('%u', $ranges['start']));<br/> }else{<br/> file_put_contents('test.log','2222',FILE_APPEND);<br/> header('HTTP/1.1 200 OK');<br/> header('content-length:'.$file_size);<br/> }<br/> <br/> while(!feof($fp)){<br/> //echo fread($fp, round($this->_speed*1024,0));<br/> //echo fread($fp, $file_size);<br/> echo fread($fp, 4096);<br/> ob_flush();<br/> }<br/> <br/> ($fp!=null) && fclose($fp);<br/> }else{<br/> return '';<br/> }<br/> }<br/> <br/> /** 设置下载速度<br/> * @param int $speed<br/> */<br/> public function setSpeed($speed){<br/> if(is_numeric($speed) && $speed>16 && $speed<4096){<br/> $this->_speed = $speed;<br/> }<br/> }<br/> <br/> /** 获取header range信息<br/> * @param int $file_size 文件大小<br/> * @return Array<br/> */<br/> private function getRange($file_size){<br/> //file_put_contents('range.log', json_encode($_SERVER), FILE_APPEND);<br/> if(isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE'])){<br/> $range = $_SERVER['HTTP_RANGE'];<br/> $range = preg_replace('/[\s|,].*/', '', $range);<br/> $range = explode('-', substr($range, 6));<br/> if(count($range)<2){<br/> $range[1] = $file_size;<br/> }<br/> $range = array_combine(array('start','end'), $range);<br/> if(empty($range['start'])){<br/> $range['start'] = 0;<br/> }<br/> if(empty($range['end'])){<br/> $range['end'] = $file_size;<br/> }<br/> return $range;<br/> }<br/> return null;<br/> }<br/>}<br/> <br/>$obj = new FileDownload();<br/>$obj->download('http://down.golaravel.com/laravel/laravel-master.zip','', true);<br/>
PHP》
以上是PHP如何實作斷點續傳大檔案?的詳細內容。更多資訊請關注PHP中文網其他相關文章!