PHP allows users to download files faster. In general, we can direct the URL to a file under DocumentRoot to guide users to download the file. However, in this way, we can't make some statistics. In general, we can direct the URL to a file under the Document Root to guide the user.
Download Files.
However, in this way, you cannot perform statistics, permission checks, and so on. Therefore, we often usePHPFor the userDownload Files.
$ 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 );
However, if the file is a Chinese name, some users may download a garbled file name.
So let's make a change (refer ::
$ File = "/tmp/Chinese name .tar.gz ";
$ Filename = basename ($ file );
Header ("Content-type: application/octet-stream ");
// Process the Chinese file name
$ 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 );
If the output is Apache + PHP mod, it needs to be sent to the output buffer of Apache. Finally sent to the user. For Nginx + fpm, if they are deployed separately, additional network IO will be introduced.
Well, it seems much better now, but there is another problem, that is, readfile. Although PHP's readfile is trying to be as efficient as possible, it does not occupy the PHP memory, but in fact, it still needs to use MMAP (if supported), or a fixed buffer to read files cyclically and output directly.
So can the Webserver directly send files to users without going through the PHP layer?
Today, I saw an interesting article: How I PHP: X-SendFile.
We can use the module mod_xsendfile of Apache to directly send this file to users:
$ File = "/tmp/Chinese name .tar.gz ";
$ Filename = basename ($ file );
Header ("Content-type: application/octet-stream ");
// Process the Chinese file name
$ 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 ).'"');
// Send an Xsendfile file
Header ("X-Sendfile: $ file ");
Lighttpd and Nginx have similar modules. if you are interested, you can find them. The X-Sendfile header is processed by Apache and the response file is directly sent to the Client.
To guide users to download files. However, in this way, you cannot make some statistics, permissions...