Implementing a file that supports the continuation of a breakpoint download the PHP class

Source: Internet
Author: User
Tags php download ranges sprintf

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:

  1. /**
  2. * Download the class, support the breakpoint continue to pass
  3. * @time: 2015 06 30 09:36
  4. * @author: guoyu@xzdkiosk.com
  5. * PHP Download class, support breakpoint continuation
  6. * Func:
  7. * Download: Download file
  8. * Setspeed: Set download speed
  9. * GetRange: Gets the range in the header
  10. */
  11. Class filedownload{//Class start
  12. Private $_speed = 512; Download speed
  13. /** Download
  14. * @param String $file The file path to download
  15. * @param String $name file name, empty is the same as the downloaded file name
  16. * @param Boolean $reload whether to turn on breakpoint continuation
  17. */
  18. Public function Download ($file, $name = ", $reload =false) {
  19. if (file_exists ($file)) {
  20. if ($name = = ") {
  21. $name = basename ($file);
  22. }
  23. $fp = fopen ($file, ' RB ');
  24. $file _size = filesize ($file);
  25. $ranges = $this->getrange ($file _size);
  26. Header (' Cache-control:public ');
  27. Header (' Content-type:application/octet-stream ');
  28. Header (' content-disposition:attachment; Filename= '. $name);
  29. if ($reload && $ranges!=null) {//Use Cont.
  30. Header (' http/1.1 206 Partial Content ');
  31. Header (' accept-ranges:bytes ');
  32. Remaining length
  33. Header (sprintf (' content-length:%u ', $ranges [' End ']-$ranges [' Start ']);
  34. Range information
  35. Header (sprintf (' Content-range:bytes%s-%s/%s ', $ranges [' Start '], $ranges [' End '], $file _size));
  36. FP pointer jumps to breakpoint position
  37. Fseek ($fp, sprintf ('%u ', $ranges [' Start ']);
  38. }else{
  39. Header (' http/1.1 OK ');
  40. Header (' Content-length: '. $file _size);
  41. }
  42. while (!feof ($fp)) {
  43. Echo fread ($fp, round ($this->_speed*1024,0));
  44. Ob_flush ();
  45. Sleep (1); For testing, slowing download speed
  46. }
  47. ($fp!=null) && fclose ($FP);
  48. }else{
  49. Return ';
  50. }
  51. }
  52. /** Setting Download speed
  53. * @param int $speed
  54. */
  55. Public Function Setspeed ($speed) {
  56. if (Is_numeric ($speed) && $speed >16 && $speed <4096) {
  57. $this->_speed = $speed;
  58. }
  59. }
  60. /** Get Header Range information
  61. * @param int $file _size file size
  62. * @return Array
  63. */
  64. Private Function GetRange ($file _size) {
  65. if (Isset ($_server[' Http_range ')) &&!empty ($_server[' Http_range ']) {
  66. $range = $_server[' Http_range ');
  67. $range = preg_replace ('/[\s|,].*/', ' ', $range);
  68. $range = Explode ('-', substr ($range, 6));
  69. if (count ($range) <2) {
  70. $range [1] = $file _size;
  71. }
  72. $range = array_combine (Array (' Start ', ' End '), $range);
  73. if (Empty ($range [' Start '])) {
  74. $range [' start '] = 0;
  75. }
  76. if (Empty ($range [' End ')]) {
  77. $range [' end '] = $file _size;
  78. }
  79. return $range;
  80. }
  81. return null;
  82. }
  83. }//Class end
  84. ?>
Copy Code
The demo sample code is as follows:
    1. Require (' FileDownload.class.php ');
    2. $file = ' Book.zip ';
    3. $name = Time (). Zip ';
    4. $obj = new FileDownload ();
    5. $flag = $obj->download ($file, $name);
    6. $flag = $obj->download ($file, $name, true); Breakpoint Continuation
    7. if (! $flag) {
    8. Echo ' File not exists ';
    9. }
    10. ?>
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

    1. $flag = $obj->download ($file, $name);
Copy Code
    1. test@ubuntu:~/downloads$ wget-o test.rar http://demo.test.com/demo.php
    2. --2013-06-30 16:52:44--htt p://demo.test.com/demo.php
    3. parsing host demo.test.com ... 127.0.0.1
    4. connecting demo.test.com|127.0.0.1|:80 ... connected.
    5. has issued an HTTP request and is waiting for a response ... $ OK
    6. Length: 10445120 (10.0M) [Application/octet-stream]
    7. saving to: "Test.rar"
    8. 3 0% [============================>] 3,146,580 513k/s estimate 14s
    9. ^c
    10. test@ubuntu:~/downloads$ wget-c -O test.rar http://demo.test.com/demo.php
    11. --2013-06-30 16:52:57--http://demo.test.com/demo.php
    12. Positive In parsing host demo.test.com ... 127.0.0.1
    13. connecting demo.test.com|127.0.0.1|:80 ... connected.
    14. has issued an HTTP request and is waiting for a response ... $ OK
    15. Length: 10445120 (10.0M) [Application/octet-stream]
    16. saving to: "Test.rar"
    17. 30% [======== ====================>] 3,146,580 515k/s estimate 14s
    18. ^c
Copy Code

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

2. Turn on breakpoint continuation

    1. $flag = $obj->download ($file, $name, true);
Copy Code
  1. test@ubuntu:~/downloads$ Wget-o Test.rar http://demo.test.com/demo.php
  2. --2013-06-30 16:53:19--http://demo.test.com/demo.php
  3. Parsing host demo.test.com ... 127.0.0.1
  4. Connecting demo.test.com|127.0.0.1|:80 ... is connected.
  5. An HTTP request has been made and is waiting for a response ... OK
  6. Length: 10445120 (10.0M) [Application/octet-stream]
  7. Saving to: "Test.rar"
  8. 20% [==================>] 2,097,720 516k/s estimate 16s
  9. ^c
  10. test@ubuntu:~/downloads$ wget-c-o test.rar http://demo.test.com/demo.php
  11. --2013-06-30 16:53:31--http://demo.test.com/demo.php
  12. Parsing host demo.test.com ... 127.0.0.1
  13. Connecting demo.test.com|127.0.0.1|:80 ... is connected.
  14. An HTTP request has been made and is waiting for a response ... 206 Partial Content
  15. Length: 10445121 (10.0M), 7822971 (7.5M) bytes remaining [Application/octet-stream]
  16. Saving to: "Test.rar"
  17. 100%[++++++++++++++++++++++++=========================================================================>] 10,445,121 543k/s Flower 14s
  18. 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
Breakpoint Continuation, PHP
  • 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.