PHP implementation file Download breakpoint continued to explain _php skills

Source: Internet
Author: User
Tags ranges

If our site provides file download services, then we usually want the download to be a breakpoint (resumable Download), which means that the user can pause the download and continue downloading from the pause at some point in the future without having to download the entire file again.

Typically, a Web server, such as Apache, opens the support for the continuation of a breakpoint by default. Therefore, if you provide a file download directly through a Web server, you can enjoy the benefits of a breakpoint renewal without having to do a special configuration. Since these files provide downloads directly through the Web server, the backend script cannot control the download process. This is not a problem for Web sites that provide only open, static files, but for Web sites that need to provide private, dynamic files, downloading directly through a Web server is not enough. At this point, you need to write a background script program, add the support for the continuation of the breakpoint.

This article will take PHP as an example, a brief introduction to the implementation of the file download breakpoint extension method.

Principle

The principle of the continuation of the breakpoint is still more intuitive.

The HTTP protocol provides a way to transfer a portion of a resource, not all of it. For example, there is a file size of 1000 bytes, the browser can request only the first 300 bytes of the file, or only request No. 500 to 1000th bytes. In this way, you do not have to transfer the entire contents of a resource in one request, but instead initiate multiple requests, requesting only part of the content at a time. After all of these requests are returned, the resulting content is pieced together to get the full resources.

The implementation of breakpoint continuation is to take advantage of the above characteristics of the HTTP protocol. When the user pauses the download, the browser records where it has been downloaded, and when the user resumes downloading at some point in the future, it is possible to continue downloading from the last paused location instead of starting from scratch.

Realize

Because the partial transfer is not mandatory, the server can support or not, so we need to tell the browser in the program whether the resource it requested supports partial transmissions. This can be done by setting the HTTP accept-ranges response header information. The PHP code is as follows:

Copy Code code as follows:

Header (' accept-ranges:bytes ');

Accept-ranges:bytes tells the browser that the resource supports partial transfers in bytes. This response header needs to be attached to all resources that support partial transmission.

When receiving a request, we need to extract the browser from the request of the browser specifically which part of the request resource. This information is passed through the Range request header. In PHP, it is stored in $_server[' Http_range '. We need to check whether this variable is defined, and if it is defined, use that value, otherwise, set the range to the entire resource.

Copy Code code as follows:

$range = "0-". ($content _length-1);
if (Isset ($_server[' Http_range ')) {
$range = $_server[' Http_range '];
}

Next, you need to analyze the value of the $range to determine which part of the resource is returned. Possible examples of values:
Copy Code code as follows:

100-200//100th to No. 200 byte
500-//NO. 500 byte to end of file
-1000//last 1000 bytes

Notice here that after you get a range, you need to examine its value, including:

1. Non-negative starting position
2. The end position needs to be greater than the start position
3. The starting position needs to be less than the file length minus one (because the location index here is starting from 0)
4. If the end position is greater than the file length minus one, you need to set its value to file length minus one

If the range value is not valid, you need to terminate the program and tell the browser:

Copy Code code as follows:

Header (' http/1.1 416 requested Range not satisfiable ');

In order to keep the article concise, the specific validation code is not provided here. The following assumes that you have checked the value of range and obtained $start and $end two variables, representing the start and end positions respectively.

The next thing to do is to send the contents of the corresponding part of the file to the browser. Note, however, that there is a need to send multiple HTTP response header information, as follows:

Copy Code code as follows:

Header (' http/1.1 206 Partial Content ');
Header (' accept-ranges:bytes ');
Header ("Content-range:bytes $start-$end/$filesize");
$length = $end-$start + 1;
Header ("Content-length: $length");

/* The specified part of the output file/*


The $length here needs to be noted that its value is the length of the contents of this transmission, not the length of the entire file. Another point to note is that the HTTP status code here is 206, not 200.

Summarize

The continuation of the breakpoint on the file download is actually taking advantage of the HTTP protocol's support for transferring parts of the file. This feature of HTTP protocol can not only be used to realize the continuity of the breakpoint, but also can be used by the client program to realize the multithread downloading.

In the process of implementing breakpoint continuation, it is necessary to pay attention to setting up various HTTP header information correctly. Incorrect header information will cause the user to download files that are corrupted and unusable.

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.