In general, we can direct the user to download files by directly having the URL point to a file located under document root.
However, in doing so, there is no way to do some statistics, permission checks, etc. work. So, a lot of times, we use to make PHP to do forwarding, to 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 this has a problem, that is, if the file is Chinese name, some users may download the file name is garbled.
So, we make a change (reference::
$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);
When the output, if it is Apache + PHP mod, then also need to send to Apache output buffer. Last sent to the user. For Nginx + FPM, if they were deployed separately, it would also bring in additional network IO.
Well, it looks a lot better now, but there is another problem, that is ReadFile, although PHP's ReadFile try to achieve as efficiently as possible without consuming PHP's own memory, but in fact it still needs to use MMAP (if supported), Or a fixed buffer to iterate through the file, directly output.
So, can not go through the PHP layer, directly to the webserver directly to send the file to the user?
Today, I saw an interesting article: how I php:x-sendfile.
We can use the Apache 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 a file
Header ("X-sendfile: $file");
LIGHTTPD and Nginx also have similar modules, we are interested can go look for
The X-sendfile header will be processed by Apache and send the response file directly to the client.