Home > Backend Development > PHP Problem > What should I do if I cannot send a large php file?

What should I do if I cannot send a large php file?

藏色散人
Release: 2023-03-17 19:22:02
Original
2046 people have browsed it

Solution to the problem that php cannot send large files: 1. Set the Range parameter with syntax such as "Ranges:(unit=first byte pos)-[last byte pos]"; 2. Set "Content-Range"; 3. Download the entire file via "GET /test.rar HTTP/1.1 Connection: close Host: 116.1.219.219" request.

What should I do if I cannot send a large php file?

The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer

What should I do if php cannot send large files? ?

PHP solution for uploading very large files

1. Overview

The so-called resume upload is actually just Refers to downloading, which means to continue downloading from the point where the file has been downloaded. Breakpoints were not supported in previous versions of the HTTP protocol, but have been supported since HTTP/1.1. Generally, the Range and Content-Range entity headers are only used for breakpoint downloads. The HTTP protocol itself does not support breakpoint upload and needs to be implemented by yourself.

2. Range

is used in the request header to specify the position of the first byte and the position of the last byte, generally Format:

Range: Used for requests from the client to the server. You can specify the size and unit of a certain segment of the downloaded file by changing the field. The byte offset starts from 0. Typical format:

    Ranges:    (unit=first byte pos)-[last byte pos]
    Ranges:    bytes=4000- 下载从第4000字节开始到文件结束部分
    Ranges:    bytes=0~N 下载第0-N字节范围的内容
    Ranges:    bytes=M-N 下载第M-N字节范围的内容
    Ranges:    bytes=-N 下载最后N字节内容
Copy after login

1. The following points need to be noted:

(1) This data interval is a closed interval, and the starting value is 0, so a request like "Range: bytes=0-1" is actually requesting the first 2 bytes.

(2) "Range: bytes=-200", it does not indicate requesting 201 bytes at the beginning of the file, but indicates requesting 200 bytes at the end of the file.

(3) If last byte pos is less than first byte pos, then this Range request is an invalid request. The server needs to ignore this Range request, then respond with a 200, and send the entire file to the client.

(4) If last byte pos is greater than or equal to the file length, then the Range request is considered unsatisfiable, and the server needs to respond with a 416, Requested range not satisfiable.

2. Example explanation:

represents the first 500 bytes: bytes=0-499

represents the second 500 bytes: bytes =500-999

Represents the last 500 bytes: bytes=-500

Represents the range after 500 bytes: bytes=500-

The first and last One byte: bytes=0-0,-1

Specify several ranges at the same time: bytes=500-600,601-999

3. Content-Range

is used in the response header to specify the insertion position of a part of the entire entity. It also indicates the length of the entire entity. Before the server returns a partial response to the client, it must describe the range covered by the response and the entire entity length. General format:

Content-Range: bytes (unit first byte pos) - [last byte pos]/[entity legth]
Copy after login

4. Header example

Request to download the entire file:

GET /test.rar HTTP/1.1 
Connection: close 
Host: 116.1.219.219 
Range: bytes=0-801 //一般请求下载整个文件是bytes=0- 或不用这个头
Copy after login

General normal response

HTTP/1.1 200 OK 
Content-Length: 801      
Content-Type: application/octet-stream 
Content-Range: bytes 0-800/801 //801:文件总大小
Copy after login

The simplest implementation of breakpoint resumption is roughly as follows:

1. The client downloads a 1024K file, of which 512K has been downloaded

2. The network is interrupted and the client requests a resumption of transmission, so it is necessary to declare in the HTTP header the fragments that need to be resumed this time:

Range:bytes=512000-
Copy after login

This header notifies the server to start transmitting the file from the 512K position of the file

3. The server receives the breakpoint resuming request, starts the transmission from the 512K position of the file, and in the HTTP header Add:

Content-Range:bytes 512000-/1024000
Copy after login

and the HTTP status code returned by the server at this time should be 206 instead of 200.

But in actual scenarios, there will be a situation, that is, when the terminal initiates a resumption request, the file content corresponding to the URL has changed on the server side, and the resuming data must be wrong. How to solve this problem? Obviously at this time we need a method to identify the uniqueness of the file. There are also corresponding definitions in RFC2616, such as implementing Last-Modified to identify the last modification time of the file, so that it can be determined whether the file has been modified when the file is resumed. At the same time, RFC2616 also defines an ETag header. You can use the ETag header to place the unique identifier of the file, such as the MD5 value of the file.

When the terminal initiates a resume request, it should declare the If-Match or If-Modified-Since field in the HTTP header to help the server identify file changes.

In addition, RFC2616 also defines an If-Range header. If the terminal continues the transmission, it uses If-Range. The content in If-Range can be the initially received ETag header or the last modified time in Last-Modfied. When the server receives the resumption request, it verifies the content in If-Range. If the verification is consistent, it returns a 206 resumption response. If it is inconsistent, the server returns a 200 response, and the response content is the entire new file. data.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What should I do if I cannot send a large php file?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template