Home  >  Article  >  Backend Development  >  Use the X-SendFile header in PHP to make file downloads faster_PHP Tutorial

Use the X-SendFile header in PHP to make file downloads faster_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:29:02726browse

Generally speaking, we can guide users to download files by directly pointing the URL to a file located under the Document Root.

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:

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 = rawurlencode($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-Length: ". filesize($file));
readfile($file);

Well, it looks much better now, 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 to read the file in a loop and output it directly.

When outputting, if it is Apache + PHP mod, it needs to be sent to the output buffer of Apache. Finally, it is sent to the user. For Nginx + fpm, if they are 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 = rawurlencode($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 . '"');
}

//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 them out

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/779166.htmlTechArticleGenerally speaking, we can guide users to download files by directly pointing the URL to a file located under the Document Root. . However, by doing this, there is no way to do some statistics and permission checks...
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