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:
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) && $speed >16 && $speed & lt;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:
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 time 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 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 ... 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
You can see that the download starts at the location of the breakpoint (%20).
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.