Use the X-SendFile header in PHP to make File Download faster

Source: Internet
Author: User
Tags php mod sendfile

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.
Copy codeThe Code is as follows:
<? Php
$ 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 the following changes:
Copy codeThe Code is as follows:
<? Php
$ 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 = rawurlencode ($ 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-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: How I PHP: X-SendFile.

We can use the module mod_xsendfile of Apache to directly send this file to users:
Copy codeThe Code is as follows:
<? Php
$ 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 = rawurlencode ($ 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 .'"');
}

// 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.

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.