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, permission checks, and so on. So, many times, we use to let PHP to do the forwarding, to provide users with file download.
Copy the Code code 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 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:
copy code code as follows:
!--? php $file = "/tmp/Chinese name. tar.gz";
$filename = basename ($file);
Header ("Content-type:application/octet-stream");
//processing Chinese file name
$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'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 needs to take mmap (if supported), Or a fixed buffer to loop through the file and output it directly.
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.
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:
Copy the Code code as follows:
$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 = 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 files
Header ("X-sendfile: $file");
The X-sendfile header is processed by Apache and the response file is sent directly to the client.
LIGHTTPD and Nginx also have similar modules, we are interested can go look for
http://www.bkjia.com/PHPjc/779166.html www.bkjia.com true http://www.bkjia.com/PHPjc/779166.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, in doing so, there is no way to do some statistics, permission check ...