Home >Backend Development >PHP Tutorial >Let PHP provide users with file downloads faster_PHP Tutorial
Generally speaking, we can guide the user to download the file by directly pointing the URL to a file located under the Document Root.
However, if you do this, you won't be able to do some statistics, permission checks, etc. Therefore, many times, we use PHP to do the forwarding and provide users with file downloads .
$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: :
$file = "/tmp/中文名.tar.gz";
$filename = basename($file);
header("Content-type: application/octet-stream");
//Process 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);
When outputting, if it is Apache + PHP mod, it also 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.
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 is used to read files cyclically and output them directly.
Then, 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:
$file = "/tmp/中文名.tar.gz";
$filename = basename($file);
header("Content-type: application/octet-stream");
//Process 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");
Lighttpd and Nginx also have similar modules. If you are interested, you can check them out. The X-Sendfile header will be processed by Apache and the response file will be sent directly to the Client.