Allow PHP to provide users with faster file downloads _ PHP Tutorial

Source: Internet
Author: User
Tags php mod sendfile
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.