Enable PHP to provide file downloads faster

Source: Internet
Author: User
Tags header php mod readfile sendfile urlencode

In general, we can direct the user to download files by directly having the URL point to a file located under document root.

However, in doing so, there is no way to do some statistics, permission checks, etc. work. So, a lot of times, we use to make PHP to do forwarding, to 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 this has a problem, that is, if the file is Chinese name, some users may download the file name is garbled.

So, we make a change (reference::

  

$file = "/tmp/Chinese name. tar.gz";

$filename = basename ($file);

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

Working with 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 the output, if it is Apache + PHP mod, then also need to send to Apache output buffer. Last sent to the user. For Nginx + FPM, if they were deployed separately, it would also bring in additional network IO.

Well, it looks a lot better now, but there is another problem, that is ReadFile, although PHP's ReadFile try to achieve as efficiently as possible without consuming PHP's own memory, but in fact it still needs to use MMAP (if supported), Or a fixed buffer to iterate through the file, directly output.

So, can not go through the PHP layer, directly to the webserver directly to send the file to the user?

Today, I saw an interesting article: how I php:x-sendfile.

We can use the Apache module Mod_xsendfile to have Apache send this file directly to the user:

  

$file = "/tmp/Chinese name. tar.gz";

$filename = basename ($file);

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

Working with 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 a file

Header ("X-sendfile: $file");

LIGHTTPD and Nginx also have similar modules, we are interested can go look for

The X-sendfile header will be processed by Apache and send the response file directly to the client.



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.