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:
- /**
- * Download the class, support the breakpoint continue to pass
- * @time: 2015 06 30 09:36
- * @author: guoyu@xzdkiosk.com
- * PHP Download class, support breakpoint continuation
- * Func:
- * Download: Download file
- * Setspeed: Set download speed
- * GetRange: Gets the range in the header
- */
- Class filedownload{//Class start
- Private $_speed = 512; Download speed
- /** Download
- * @param String $file The 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 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 Cont.
- 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));
- FP pointer jumps to 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 ';
- }
- }
- /** Setting Download speed
- * @param int $speed
- */
- Public Function Setspeed ($speed) {
- if (Is_numeric ($speed) && $speed >16 && $speed <4096) {
- $this->_speed = $speed;
- }
- }
- /** Get Header Range information
- * @param int $file _size 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
- ?>
Copy Code The demo sample code is as follows:
- Require (' FileDownload.class.php ');
- $file = ' Book.zip ';
- $name = Time (). Zip ';
- $obj = new FileDownload ();
- $flag = $obj->download ($file, $name);
- $flag = $obj->download ($file, $name, true); Breakpoint Continuation
- if (! $flag) {
- Echo ' File not exists ';
- }
- ?>
Copy Code 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);
Copy Code
- test@ubuntu:~/downloads$ wget-o test.rar http://demo.test.com/demo.php
- --2013-06-30 16:52:44--htt p://demo.test.com/demo.php
- parsing host demo.test.com ... 127.0.0.1
- connecting demo.test.com|127.0.0.1|:80 ... connected.
- has issued an HTTP request and is waiting for a response ... $ OK
- Length: 10445120 (10.0M) [Application/octet-stream]
- saving to: "Test.rar"
-
- 3 0% [============================>] 3,146,580 513k/s estimate 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
- Positive In parsing host demo.test.com ... 127.0.0.1
- connecting demo.test.com|127.0.0.1|:80 ... connected.
- has issued an HTTP request and is waiting for a response ... $ OK
- Length: 10445120 (10.0M) [Application/octet-stream]
- saving to: "Test.rar"
- 30% [======== ====================>] 3,146,580 515k/s estimate 14s
- ^c
Copy Code As you can see, wget-c cannot be resumed on a breakpoint. 2. Turn on breakpoint continuation
- $flag = $obj->download ($file, $name, true);
Copy Code
- 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 estimate 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
- 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])
Copy Code You can see that the download starts at the location of the breakpoint (%20). From: http://blog.csdn.net/phpfenghuo/article/details/46691865
|