In general, we can direct the URL to a file under DocumentRoot to guide users to download the file.
In general, we can direct the URL to a file under the Document Root to guide users to download the file.
However, in this way, we can't do some statistics, permission checks, and so on. So, in many cases, we use PHP for forwarding to provide users with file downloads.
The Code is 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 );
However, if the file is a Chinese name, some users may download a garbled file name.
So let's make a change (refer ::
The Code is as follows:
$ 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 );
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 the file cyclically and directly output the 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.
So can the Webserver directly send files to users without going through the PHP layer?
Today, I saw an interesting article :.
We can use the Apache module to allow Apache to directly send this file to users:
The Code is as follows:
$ 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 ");
The X-Sendfile header is processed by Apache and the response file is directly sent to the Client.
Lighttpd and Nginx have similar modules. If you are interested, you can find them.