Php supports resumable file downloads,

Source: Internet
Author: User
Tags php download

Php supports resumable file downloads,

The example in this article describes the php-enabled resumable object download class and its usage. It is a very practical technique. Share it with you for your reference. The specific method is as follows:

Generally, php supports resumable data transfer, mainly based on the HTTP header HTTP_RANGE.

Principle of HTTP resumable upload:

Http header Range, Content-Range ()
Generally, the Range and Content-Range object headers are used for resumable download in the HTTP header,
In the Range user request header, specify the location of the first byte and the location of the last byte, for example (Range: 200-300)
Content-Range is used 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 // the general request for downloading the entire object is bytes = 0-or you do not need this Header

Normal response:

HTTP/1.1 200 OK
Content-Length: 801
Content-Type: application/octet-stream
Content-Range: bytes 0-800/801 // 801: total file size

The FileDownload. class. php file code is as follows:

<? Php/** php download class, supports resumable upload * Date: 2013-06-30 * Author: test * Ver: 1.0 ** Func: * download: download file * setSpeed: set the download speed * getRange: Get the Range */class FileDownload in the header {// class start private $ _ speed = 512; // download speed/** download * @ param String $ file the path of the file to be downloaded * @ param String $ name, if it is null, it is the same as the name of the downloaded file * @ param boolean $ reload: Indicates whether to enable resumable upload */public function download ($ file, $ name = '', $ reload = false) {if (file_exists ($ fi Le) {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 the resume 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 jump to the breakpoint position 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 ); // used for testing to slow down the 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 <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?>

The demo 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); // resumable upload if (! $ Flag) {echo 'file not exists';}?>

Resumable upload Test method:

Run the linux wget command to test the download. wget-c-O file http: // xxx

1. Disable resumable upload.

$flag = $obj->download($file, $name);
Test @ ubuntu :~ /Downloads $ wget-O test.rar http://demo.test.com/demo.php -- 2013-06-30 16:52:44 -- The http://demo.test.com/demo.php is parsing the host demo.test.com... 127.0.0.1 is connecting to demo.test.com | 127.0.0.1 |: 80... connected. An HTTP request has been sent and is waiting for response... 200 OK length: 10445120 (10.0 M) [application/octet-stream] is being saved: export test.rar "30% [==============================>] 3,146,580 513 K/s estimated time 14 s ^ C test @ ubuntu: ~ /Downloads $ wget-c-O test.rar http://demo.test.com/demo.php -- 2013-06-30 16:52:57 -- The http://demo.test.com/demo.php is parsing the host demo.test.com... 127.0.0.1 is connecting to demo.test.com | 127.0.0.1 |: 80... connected. An HTTP request has been sent and is waiting for response... 200 OK length: 10445120 (10.0 M) [application/octet-stream] is being saved: export test.rar "30% [==============================>] 3,146,580 515 K/s estimated time 14 s ^ C

As you can see, wget-c cannot be resumed.

2. Enable resumable upload

$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 -- The http://demo.test.com/demo.php is parsing the host demo.test.com... 127.0.0.1 is connecting to demo.test.com | 127.0.0.1 |: 80... connected. An HTTP request has been sent and is waiting for response... 200 OK length: 10445120 (10.0 M) [application/octet-stream] is being saved: export test.rar "20% [==========================>] 2,097,720 516 K/s estimated 16 s ^ C test @ ubuntu :~ /Downloads $ wget-c-O test.rar http://demo.test.com/demo.php -- 2013-06-30 16:53:31 -- The http://demo.test.com/demo.php is parsing the host demo.test.com... 127.0.0.1 is connecting to demo.test.com | 127.0.0.1 |: 80... connected. An HTTP request has been sent and is waiting for response... 206 Partial Content Length: 10445121 (10.0 M), 7822971 (7.5 M) bytes remaining [application/octet-stream] are being saved: export test.rar "100% [++ ============ ========================================================== ===========>] 10,445,121 543 K/s flowers 14 s 16:53:45 (543 KB/s) -Saved export test.rar "[10445121/10445121])

We can see that the download starts from the breakpoint position (% 20.

You can click here to download the complete source code of this example.

I believe this article has some reference value for everyone's PHP program design.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.