Home >Backend Development >PHP Tutorial >Let PHP provide file download code faster_PHP tutorial

Let PHP provide file download code faster_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:18:14826browse

However, by doing this, there is no way to do some statistics, permission checks, etc. Therefore, many times, we use PHP to do the forwarding and provide users with file downloads.

Copy code The code is as follows:

$file = "/tmp/dummy.tar.gz";
header("Content- type: application/octet-stream"); ​​
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header("Content-Length: ". filesize($file));
readfile($file);

But there is a problem with this, that is, if the file has a Chinese name, some users may download the file name with garbled characters. .

So, let’s make some modifications (reference: :
Copy code The code is as follows:

< ;?php
$file = "/tmp/中文名.tar.gz";
$filename = basename($file);
header("Content-type: application/octet-stream") ;
//Processing Chinese file names
$ua = $_SERVER["HTTP_USER_AGENT"];
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "% 20", $encoded_filename);
if (preg_match("/MSIE/", $ua)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header("Content-Disposition: attachment; filename*="utf8''" . $filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
header('Content-Disposition: attachment; filename="' . $filename . '"');
header("Content-Length: ". filesize($file));
readfile($file);

Well, look now It looks much better, but there is still a problem, and that is readfile. Although PHP's readfile tries to be as efficient as possible and does not occupy PHP's own memory, in fact it still needs to use MMAP (if supported), or a fixed buffer Read the file in a loop and output it directly.

When outputting, if it is Apache + PHP mod, then it needs to be sent to the output buffer of Apache. Finally, it is sent to the user. For Nginx + fpm, if they If deployed separately, it will also bring additional network IO.

So, can the Webserver directly send the file to the user without going through the PHP layer?

Today, I saw an interesting article: How I PHP: X-SendFile.

We can use Apache's module mod_xsendfile to let Apache send this file directly to the user:

Copy the code The code is as follows:

$file = "/tmp/中文名.tar.gz";
$filename = basename($file);
header("Content-type: application /octet-stream"); ​​
//Processing Chinese file names
$ua = $_SERVER["HTTP_USER_AGENT"];
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace( "+", "%20", $encoded_filename);
if (preg_match("/MSIE/", $ua)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header("Content-Disposition: attachment; filename*="utf8''" . $filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
//Let Xsendfile send the file
header("X-Sendfile: $file");

The X-Sendfile header will be processed by Apache and the response file will be sent directly to the Client.
Lighttpd and Nginx also have similar modules. If you are interested, you can check it out

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325558.htmlTechArticleHowever, by doing this, there is no way to do some statistics, permission checks, etc. Therefore, many times, , we use PHP to do the forwarding and provide users with file downloads. Copy the code like...
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