In general, we can direct the user by directing the URL directly to a file located under the document root
Download File。
However, to do so, there is no way to do some statistics, permission checks, and so on. So, many times, we use to let PHP to do the forwarding, to provide users with file download .
$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 this has a problem, that is, if the file is a Chinese name, some users may download the file name is garbled.
So, let's make a change (see:
$file = "/tmp/Chinese name. tar.gz";
$filename = basename ($file);
Header ("Content-type:application/octet-stream");
Working with 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);
Output, if it is Apache + PHP mod, then also need to send to Apache output buffer. Finally sent to the user. and for Nginx + FPM If they are deployed separately, that will also bring additional network IO.
Well, it looks much better now, but there's another problem, that is, ReadFile, although PHP's ReadFile try to be as efficient as possible, without taking up PHP's own memory, but in fact it still needs to adopt mmap (if supported), Or a fixed buffer to iterate through the file and output it directly.
So, can not go through this layer of PHP, directly let webserver directly send the file to the user?
Today, I saw an interesting article: how I php:x-sendfile.
We can use Apache's module mod_xsendfile to have Apache send this file directly to the user:
$file = "/tmp/Chinese name. tar.gz";
$filename = basename ($file);
Header ("Content-type:application/octet-stream");
Working with 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 files
Header ("X-sendfile: $file");
LIGHTTPD and Nginx also have similar modules, everyone is interested can go look for. The X-sendfile header is processed by Apache and the response file is sent directly to the client.
http://www.bkjia.com/PHPjc/371720.html www.bkjia.com true http://www.bkjia.com/PHPjc/371720.html techarticle In general, we can direct the user to download a file by directly directing the URL to a file located under document root. However, to do so, there is no way to do some statistics, permissions ...