PHP implements resumable Upload for file downloads _ php skills

Source: Internet
Author: User
This article mainly introduces how to implement resumable upload and download in PHP. This article explains how to implement resumable Upload and provides the implementation code, for more information, see. if our website provides the file Download service, we usually want to Download the service with Resumable Download, which means users can pause the Download, and resume the download from the pause at a certain time in the future, instead of downloading the entire file again.

Generally, Web servers (such as Apache) enable resumable data transfer by default. Therefore, if you directly use a Web server to download files, you can enjoy the benefits of resumable data transfer without having to make special configurations. Because these files are directly downloaded through the Web server, the backend script cannot control the download process. This is not a problem for websites that only provide public and static files, but for websites that need to provide private and dynamic files, direct download via Web servers cannot meet the requirements. In this case, you need to add support for resumable Upload when writing background scripts.

This article takes PHP as an example to briefly introduce how to implement resumable download.

Principle

The principle of resumable data transfer is more intuitive.

The HTTP protocol specifies how to transmit part of a resource, not all. For example, if the size of a file is 1000 bytes, the browser can request only the first 300 bytes of the file or only 500th to 1,000th bytes. In this way, you do not have to transmit all the content of a resource in one request, but initiate multiple requests, each request only requests part of the content. After all these requests are returned, splice the obtained content together to obtain the complete resource.

To implement resumable data transfer, you must use the above features of the HTTP protocol. When the user suspends the download, the browser will record the location where the download has been made. when the user resumes the download at a certain time in the future, the download can continue from the last paused location, instead of starting from scratch.

Implementation

Because some transmission is not mandatory, the server may or may not support it. Therefore, we need to tell the browser in the program whether the requested resources support partial transmission. This can be achieved 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 this resource supports partial transmission in bytes. This response header must be appended to all resources that support partial transmission.

When receiving a request, we need to extract from the browser request which part of the resource is requested by the browser. This information is transmitted through the Range request header. In PHP, it is stored in $ _ SERVER ['http _ range. We need to check whether this variable is defined. if so, use this value. otherwise, set 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 $ range to determine which part of the returned resource content. Possible value examples:

The code is as follows:


100-200 // 100th to 200th bytes
500-// 500th bytes to the end of the file
-1000 // The final 1000 bytes


Note that after obtaining a Range, you need to test its value, including:

1. non-negative start position
2. the end position must be greater than the start position.
3. the start position must be less than the file length minus one (because the index starts from 0)
4. if the end position is greater than the file length minus one, you need to set its value to the file length minus one.

If the Range value is invalid, terminate the program and notify the browser:

The code is as follows:


Header ('http/1.1 416 Requested Range Not satisfiable ');


To keep the article concise, the specific verification code is not provided here. The following assumes that you have verified the Range value and obtained the $ start and $ end Variables, respectively indicating the start position and end position.

The next step is to send the content of the corresponding part of the file to the browser. However, it should be noted that multiple HTTP response headers need to be sent here, as shown below:

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 ");

/* Specified part of the output file */


Pay attention to the $ length here. its value is the length of the content transmitted this time, rather than the length of the entire file. Note that the HTTP status code is 206, not 200.

Summary

Resumable Upload of object downloads uses the support for transmission of some objects in the HTTP protocol. The HTTP protocol can be used not only for resumable data transfer, but also for multi-thread download.

In the process of resumable Upload, you must correctly set various HTTP header information. Wrong header information will cause damage to the file downloaded by the user, and it cannot be used.

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.