PHP implementation of the support for the continuation of the file download class,
This article describes the PHP implementation of support for the continuation of the file download class and its usage, is a very practical technique. Share to everyone for your reference. Here's how:
Generally speaking, PHP supports the continuation of breakpoints, mainly rely on the HTTP protocol header Http_range implementation.
HTTP Breakpoint Continuation principle:
HTTP header Range, Content-range ()
The range and Content-range entity headers are used in HTTP headers for general breakpoint downloads.
Range user Request header, specify the position of the first byte and the position of the last byte, such as (range:200-300)
Content-range for response headers
Request to download the entire file:
Get/test.rar http/1.1
Connection:close
host:116.1.219.219
range:bytes=0-801//General request to download the entire file is bytes=0-or without this header
General Normal Response:
http/1.1 OK
content-length:801
Content-type:application/octet-stream
Content-range:bytes 0-800/801//801: Total File size
The FileDownload.class.php class file code is as follows:
<?php/** PHP Download class, support breakpoint continuation * date:2013-06-30 * author:test * ver:1.0 * * Func: * Download: Download file * setspeed: Settings under Load speed * GetRange: Get Header Range */class filedownload{//class start Private $_speed = 512; Download speed/** Download * @param string $file file path to download * @param string $name file name, empty is the same as the downloaded file name * @param boolean $reload Whether to turn on the breakpoint continuation */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) {//Use continuation header (' http/1.1 206 Partial Content '); Header (' accept-ranges:bytes '); Remaining Length header (sprintf (' content-length:%u ', $ranges ['End ']-$ranges [' Start ']); Range Information Header (sprintf (' Content-range:bytes%s-%s/%s ', $ranges [' Start '], $ranges [' End '], $file _size)); The FP pointer jumps to the breakpoint position fseek ($fp, sprintf ('%u ', $ranges [' Start ']); }else{header (' http/1.1 OK '); Header (' Content-length: '. $file _size); } while (!feof ($fp)) {echo fread ($fp, round ($this->_speed*1024,0)); Ob_flush (); Sleep (1); For testing, slowing download speed} ($fp!=null) && fclose ($FP); }else{return '; }}/** set download speed * @param int $speed */Public Function setspeed ($speed) {if (Is_numeric ($speed) && $s peed>16 && $speed <4096) {$this->_speed = $speed; }}/** gets header range information * @param int $file _size File size * @return Array */Private Function GetRange ($file _siz e) {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?>
The demo sample code is as follows:
<?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); The breakpoint continues to pass if (! $flag) { echo ' file not exists ';}?>
Test methods for the continuation of breakpoints:
Use the Linux wget command to test the download, wget-c-o file http://xxx
1. Turn off the continuation of the breakpoint first
$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 Parsing host demo.test.com ... 127.0.0.1 Connecting demo.test.com|127.0.0.1|:80 ... is connected. An HTTP request has been made and is waiting for a response ... $ OK Length: 10445120 (10.0M) [Application/octet-stream] saving to: "Test.rar" 30% [============================> ] 3,146,580 513k/s estimated 14s ^c test@ubuntu:~/downloads$ wget-c-o test.rar http://demo.test.com/demo.php--2013-0 6-30 16:52:57--http://demo.test.com/demo.php is parsing host demo.test.com ... 127.0.0.1 Connecting demo.test.com|127.0.0.1|:80 ... is connected. An HTTP request has been made and is waiting for a response ... $ OK Length: 10445120 (10.0M) [Application/octet-stream] saving to: "Test.rar" 30% [============================> ] 3,146,580
As you can see, wget-c cannot be resumed on a breakpoint.
2. Turn on breakpoint continuation
$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 Parsing host demo.test.com ... 127.0.0.1 Connecting demo.test.com|127.0.0.1|:80 ... is connected. An HTTP request has been made and is waiting for a response ... $ OK Length: 10445120 (10.0M) [Application/octet-stream] saving to: "Test.rar" 20% [==================> ] 2,097,720 516K/S estimated 16s ^c test@ubuntu:~/downloads$ wget-c-o test.rar http://demo.test.com/demo.ph P--2013-06-30 16:53:31--http://demo.test.com/demo.php parsing host demo.test.com ... 127.0.0.1 Connecting demo.test.com|127.0.0.1|:80 ... is connected. An HTTP request has been made and is waiting for a response ... 206 Partial Content Length: 10445121 (10.0M), 7822971 (7.5M) bytes remaining [Application/octet-stream] saving to: "Test.rar" 100%[++++++++++ ++++++++++++++=========================================================================>] 10,445,121 543K/s Flower 14s 2013-06-30 16:53:45 (543 kb/s)-Saved "Test.rar" [10445121/10445121])
You can see that the download starts at the location of the breakpoint (%20).
This article example full source can click here to download the site.
It is believed that this article has certain reference value to the PHP program design of everybody.
http://www.bkjia.com/PHPjc/882897.html www.bkjia.com true http://www.bkjia.com/PHPjc/882897.html techarticle PHP Implementation of the support for the continuation of the file download class, the example of this article describes the PHP implementation of support for the continuation of the file download class and its use, is a very practical technique. Share for the big ...