Home  >  Article  >  Backend Development  >  Let PHP provide users with file downloads faster_PHP Tutorial

Let PHP provide users with file downloads faster_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 14:51:471010browse

Generally speaking, we can guide the user to download the file by directly pointing the URL to a file located under the Document Root.


However, if you do this, you won't be able to do some statistics, permission checks, etc. Therefore, many times, we use PHP to do the forwarding and 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 there is a problem with this, that is, if the file has a Chinese name, some users may download the file name with garbled characters.


So, let’s make some modifications (reference: :


$file = "/tmp/中文名.tar.gz";


$filename = basename($file);


header("Content-type: application/octet-stream");


//Process 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 outputting, if it is Apache + PHP mod, it also needs to be sent to the output buffer of Apache. Finally it is sent to the user. For Nginx + fpm, if they are deployed separately, it will also bring additional network IO.


Well, it looks much better now, but there is still a problem, and that is readfile. Although PHP's readfile tries to be as efficient as possible and does not occupy PHP's own memory, in fact it still needs to use MMAP (if supported), or A fixed buffer is used to read files cyclically and output them directly.

Then, can the Webserver directly send the file to the user without going through the PHP layer?


Today, I saw an interesting article: How I PHP: X-SendFile.


We can use Apache's module mod_xsendfile to let Apache send this file directly to the user:


$file = "/tmp/中文名.tar.gz";


$filename = basename($file);


header("Content-type: application/octet-stream");


//Process 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 the file


header("X-Sendfile: $file");


Lighttpd and Nginx also have similar modules. If you are interested, you can check them out. The X-Sendfile header will be processed by Apache and the response file will be sent directly to the Client.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371720.htmlTechArticleGenerally speaking, we can guide users to download files by directly pointing the URL to a file located under the Document Root. . However, if you do this, you will not be able to do some statistics and permissions...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn