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.
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字节内容
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]
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- 或不用这个头
General normal response
HTTP/1.1 200 OK Content-Length: 801 Content-Type: application/octet-stream Content-Range: bytes 0-800/801 //801:文件总大小
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-
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
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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools