php實(shí)現(xiàn)的支持?jǐn)帱c(diǎn)續(xù)傳的文件下載類
本文實(shí)例講述了php實(shí)現(xiàn)的支持?jǐn)帱c(diǎn)續(xù)傳的文件下載類及其用法,是非常實(shí)用的技巧。分享給大家供大家參考。具體方法如下:
通常來說,php支持?jǐn)帱c(diǎn)續(xù)傳,主要依靠HTTP協(xié)議中 header HTTP_RANGE實(shí)現(xiàn)。
HTTP斷點(diǎn)續(xù)傳原理:
Http頭 Range、Content-Range()
HTTP頭中一般斷點(diǎn)下載時(shí)才用到Range和Content-Range實(shí)體頭,
Range用戶請(qǐng)求頭中,指定第一個(gè)字節(jié)的位置和最后一個(gè)字節(jié)的位置,如(Range:200-300)
Content-Range用于響應(yīng)頭
請(qǐng)求下載整個(gè)文件:
GET /test.rar HTTP/1.1
Connection: close
Host: 116.1.219.219
Range: bytes=0-801 //一般請(qǐng)求下載整個(gè)文件是bytes=0- 或不用這個(gè)頭
一般正?;貞?yīng):
HTTP/1.1 200 OK
Content-Length: 801
Content-Type: application/octet-stream
Content-Range: bytes 0-800/801 //801:文件總大小
FileDownload.class.php類文件代碼如下:
<?php
/** php下載類,支持?jǐn)帱c(diǎn)續(xù)傳
* Date: 2013-06-30
* Author: test
* Ver: 1.0
*
* Func:
* download: 下載文件
* setSpeed: 設(shè)置下載速度
* getRange: 獲取header中Range
*/
class FileDownload{ // class start
private $_speed = 512; // 下載速度
/** 下載
* @param String $file 要下載的文件路徑
* @param String $name 文件名稱,為空則與下載的文件名稱一樣
* @param boolean $reload 是否開啟斷點(diǎn)續(xù)傳
*/
public function download($file, $name='', $reload=false){
if(file_exists($file)){
if($name==''){
$name = basename($file);
}
$fp = fopen($file, 'rb');
$file_size = filesize($file);
$ranges = $this->getRange($file_size);
header('cache-control:public');
header('content-type:application/octet-stream');
header('content-disposition:attachment; filename='.$name);
if($reload && $ranges!=null){ // 使用續(xù)傳
header('HTTP/1.1 206 Partial Content');
header('Accept-Ranges:bytes');
// 剩余長度
header(sprintf('content-length:%u',$ranges['end']-$ranges['start']));
// range信息
header(sprintf('content-range:bytes %s-%s/%s', $ranges['start'], $ranges['end'], $file_size));
// fp指針跳到斷點(diǎn)位置
fseek($fp, sprintf('%u', $ranges['start']));
}else{
header('HTTP/1.1 200 OK');
header('content-length:'.$file_size);
}
while(!feof($fp)){
echo fread($fp, round($this->_speed*1024,0));
ob_flush();
//sleep(1); // 用于測(cè)試,減慢下載速度
}
($fp!=null) && fclose($fp);
}else{
return '';
}
}
/** 設(shè)置下載速度
* @param int $speed
*/
public function setSpeed($speed){
if(is_numeric($speed) && $speed>16 && $speed<4096){
$this->_speed = $speed;
}
}
/** 獲取header range信息
* @param int $file_size 文件大小
* @return Array
*/
private function getRange($file_size){
if(isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE'])){
$range = $_SERVER['HTTP_RANGE'];
$range = preg_replace('/[\s|,].*/', '', $range);
$range = explode('-', substr($range, 6));
if(count($range)<2){
$range[1] = $file_size;
}
$range = array_combine(array('start','end'), $range);
if(empty($range['start'])){
$range['start'] = 0;
}
if(empty($range['end'])){
$range['end'] = $file_size;
}
return $range;
}
return null;
}
} // class end
?>
demo示例代碼如下:
<?php
require('FileDownload.class.php');
$file = 'book.zip';
$name = time().'.zip';
$obj = new FileDownload();
$flag = $obj->download($file, $name);
//$flag = $obj->download($file, $name, true); // 斷點(diǎn)續(xù)傳
if(!$flag){
echo 'file not exists';
}
?>
斷點(diǎn)續(xù)傳測(cè)試方法:
使用linux wget命令去測(cè)試下載, wget -c -O file http://xxx
1.先關(guān)閉斷點(diǎn)續(xù)傳
$flag = $obj->download($file, $name);
test@ubuntu:~/Downloads$ wget -O test.rar http://demo.test.com/demo.php --2013-06-30 16:52:44-- http://demo.test.com/demo.php 正在解析主機(jī) demo.test.com... 127.0.0.1 正在連接 demo.test.com|127.0.0.1|:80... 已連接。 已發(fā)出 HTTP 請(qǐng)求,正在等待回應(yīng)... 200 OK 長度: 10445120 (10.0M) [application/octet-stream] 正在保存至: “test.rar” 30% [============================> ] 3,146,580 513K/s 估時(shí) 14s ^C test@ubuntu:~/Downloads$ wget -c -O test.rar http://demo.test.com/demo.php --2013-06-30 16:52:57-- http://demo.test.com/demo.php 正在解析主機(jī) demo.test.com... 127.0.0.1 正在連接 demo.test.com|127.0.0.1|:80... 已連接。 已發(fā)出 HTTP 請(qǐng)求,正在等待回應(yīng)... 200 OK 長度: 10445120 (10.0M) [application/octet-stream] 正在保存至: “test.rar” 30% [============================> ] 3,146,580 515K/s 估時(shí) 14s ^C
可以看到,wget -c不能斷點(diǎn)續(xù)傳
2.開啟斷點(diǎn)續(xù)傳
$flag = $obj->download($file, $name, true);
test@ubuntu:~/Downloads$ wget -O test.rar http://demo.test.com/demo.php --2013-06-30 16:53:19-- http://demo.test.com/demo.php 正在解析主機(jī) demo.test.com... 127.0.0.1 正在連接 demo.test.com|127.0.0.1|:80... 已連接。 已發(fā)出 HTTP 請(qǐng)求,正在等待回應(yīng)... 200 OK 長度: 10445120 (10.0M) [application/octet-stream] 正在保存至: “test.rar” 20% [==================> ] 2,097,720 516K/s 估時(shí) 16s ^C test@ubuntu:~/Downloads$ wget -c -O test.rar http://demo.test.com/demo.php --2013-06-30 16:53:31-- http://demo.test.com/demo.php 正在解析主機(jī) demo.test.com... 127.0.0.1 正在連接 demo.test.com|127.0.0.1|:80... 已連接。 已發(fā)出 HTTP 請(qǐng)求,正在等待回應(yīng)... 206 Partial Content 長度: 10445121 (10.0M),7822971 (7.5M) 字節(jié)剩余 [application/octet-stream] 正在保存至: “test.rar” 100%[++++++++++++++++++++++++=========================================================================>] 10,445,121 543K/s 花時(shí) 14s 2013-06-30 16:53:45 (543 KB/s) - 已保存 “test.rar” [10445121/10445121])
可以看到會(huì)從斷點(diǎn)的位置(%20)開始下載。
本文實(shí)例完整源碼可點(diǎn)擊此處本站下載。
相信本文所述對(duì)大家的PHP程序設(shè)計(jì)有一定的借鑒價(jià)值。
相關(guān)文章
php實(shí)現(xiàn)mysql同步的實(shí)現(xiàn)方法
由于公司的英文網(wǎng)站放置在美國,而這些網(wǎng)站的數(shù)據(jù)要與大陸的服務(wù)器數(shù)據(jù)同步。 同步時(shí)間在一天之內(nèi)。2009-10-10
phpStudy中升級(jí)MySQL版本到5.7.17的方法步驟
這篇文章主要給大家介紹了關(guān)于phpStudy中升級(jí)MySQL版本到5.7.17的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-08-08
PHP Undefined index報(bào)錯(cuò)的修復(fù)方法
用$_GET["xx"]這種形式取得數(shù)據(jù)時(shí),如果之前不加判斷,$_GET["xx"]不存在時(shí)會(huì)出現(xiàn)這樣的警告:PHP Notice: undefined index xxx。2011-07-07
php不使用copy()函數(shù)復(fù)制文件的方法
這篇文章主要介紹了php不使用copy()函數(shù)復(fù)制文件的方法,涉及php讀寫文件的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
php實(shí)現(xiàn)與python進(jìn)行socket通信的方法示例
這篇文章主要介紹了php實(shí)現(xiàn)與python進(jìn)行socket通信的方法,結(jié)合實(shí)例形式分析了php使用自定義類發(fā)送socket請(qǐng)求數(shù)據(jù)及Python接收socket數(shù)據(jù)并處理請(qǐng)求等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08

