Home >Backend Development >PHP Problem >What to do if there is an error when downloading large files in PHP

What to do if there is an error when downloading large files in PHP

藏色散人
藏色散人Original
2022-11-13 09:47:121811browse

Solution to the error when php downloads large files: 1. View the nginx log on the server; 2. View the fpm running identity; 3. Edit the nginx configuration to set the identity; 4. Pass "chown -R daemon / aichenk/soft/nginx/fastcgi_temp/" just change the path.

What to do if there is an error when downloading large files in PHP

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

What should I do if there is an error when downloading a large file in php?

Remember a pitfall when PHP failed to download large files

Explanation

php provides file storage and downloading, nginx serves as the web server, and fpm does the parsing.

Phenomena

When downloading a 5M picture, it always prompts that the download failed, or the downloaded file is incomplete and only part of the image is displayed (different for each download)

phpDownload related code

$file = BASE_PATH . '/public/files/IMG_5727.jpg';
$size = filesize($file);
header( "Content-type: application/octet-stream" );
header("Accept-Ranges: bytes");
header("Accept-Length: {$size}");
header("Content-Disposition: attachment; filename=IMG_5727.jpg");
$fp = fopen($file, 'rb');
$readBuffer = 4096;
while (!feof($fp)) {
    echo fread($fp, $readBuffer);
}
fclose($fp);

Solution

After searching around, I went to the server to check the nginx log, and there was indeed an error message

2020/05/06 13:09:13 [crit] 1482#0: *23258 open() "/aichenk/soft/nginx/fastcgi_temp/4/17/0000000174" failed (13: Permis
sion denied) while reading upstream, client: 172.21.0.12, server: 192.168.5.5, request: "GET /temp/t2 HTTP/1.1", upstr
eam: "fastcgi://127.0.0.1:9001", host: "192.168.5.5:8080"

nginx shows permission problems, and finally unifies the nginx running identity and fpm

# fpm配置
[www]
user = daemon
group = daemon
 
# 查看到fpm运行身份为`daemon`,编辑nginx配置设置身份
 
# nginx配置
#user  nobody;
user daemon;

The previous path ownership user changed

chown -R daemon /aichenk/soft/nginx/fastcgi_temp/

Test passed

Explanation

Personal analysis: This error is actually reflected when the data is placed in the buffer. In theory, not only large files, but also data transmission volume exceeding a certain value will have this problem. Just pay attention when setting up the environment.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What to do if there is an error when downloading large files in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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