If our site provides file download services, then usually we would like to download a breakpoint continuation (Resumable Download), that is, the user can pause the download, and at some time in the future to continue downloading from the pause, without having to re-download the entire file.
Typically, Web servers (such as Apache) turn on 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 continuation without having to make a special configuration. Since these files are available for download directly through the Web server, the back-end script cannot control the download process. This is not a problem for Web sites that provide only public, static files, but for websites that need to provide private, dynamic files, downloading directly from a Web server is not enough. At this point, you need to add support for the continuation of a breakpoint when writing a background script.
This article will take PHP as an example, briefly introduce the implementation of the file download breakpoint continuation method.
Principle
The principle of the continuation of the breakpoint is more intuitive.
The HTTP protocol specifies how to transfer a portion of a resource, not all. For example, the size of a file is 1000 bytes, and the browser can request only the first 300 bytes of the file, or only the No. 500 to 1000th bytes. In this way, you do not have to transfer the entire contents of a resource in a single request, but instead initiate multiple requests, requesting only a subset of the content at a time. After all these requests are returned, the resulting content is stitched together to get the complete resource.
The implementation of the breakpoint continuation is to take advantage of the HTTP protocol of the above features. When the user pauses the download, the browser records where it has been downloaded, and when the user resumes the download at some point in the future, it can continue downloading from the last paused location without having to start from scratch.
Realize
Because the partial transfer is not mandatory, the server can support it or not, so we need to tell the browser in the program whether the requested resource supports partial transmission. This can be done by setting the HTTP accept-ranges response header information. The PHP code is as follows:
The code is 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 transfers.
When a request is received, we need to extract the browser from the browser's request specifically in which part of the resource is requested. This information is passed through the Range request header. In PHP, it is stored in $_server[' Http_range ']. We need to check if this variable is defined, and if so, use that value, or set the range to the entire resource.
The code is 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. Examples of possible values:
The code is as follows:
100-200//100th to No. 200 bytes
500-//NO. 500 byte to end of file
-1000//last 1000 bytes
It is important to note that after you get a range, you need to test its value, including:
1. Start position non-negative
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 position 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 value of range is not valid, you need to terminate the program and tell the browser:
The code is as follows:
Header (' http/1.1 416 requested Range not satisfiable ');
In order to keep the article concise, the specific check code is not provided here. The following assumes that you have checked the value of range and obtained the $start and $end two variables representing the starting and ending 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 this involves the need to send multiple HTTP response header information, as follows:
The code is 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 thing to note is that the HTTP status code here is 206, not 200.
Summarize
The file download's breakpoint continuation is actually taking advantage of the HTTP protocol to support the transmission of some files. This feature of the HTTP protocol can be used not only to implement the continuation of the breakpoint, but also to enable the client program to use it for multi-threaded downloading.
In the process of implementing a breakpoint, it is necessary to pay attention to the proper setting of various HTTP header information. Incorrect header information will cause users to download files that are corrupt and unusable.
Original link: http://www.kubiji.cn/juhe-id3146.html
PHP implementation file Download breakpoint continuation of the detailed