Allow PHP to provide users with a faster file download _php tutorial

Source: Internet
Author: User
Tags php mod readfile sendfile urlencode
In general, we can direct the user by directing the URL directly to a file located under the document root Download File


However, to do so, there is no way to do some statistics, permission checks, and so on. So, many times, we use to let PHP to do the forwarding, to provide users with file download .


$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 a Chinese name, some users may download the file name is garbled.


So, let's make a change (see:


$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);


Output, if it is Apache + PHP mod, then also need to send to Apache output buffer. Finally sent to the user. and for Nginx + FPM If they are deployed separately, that will also bring additional network IO.


Well, it looks much better now, but there's another problem, that is, ReadFile, although PHP's ReadFile try to be as efficient as possible, without taking up PHP's own memory, but in fact it still needs to adopt mmap (if supported), Or a fixed buffer to iterate through the file and output it directly.

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


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


We can use Apache's 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 files


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


LIGHTTPD and Nginx also have similar modules, everyone is interested can go look for. The X-sendfile header is processed by Apache and the response file is sent directly to the client.

http://www.bkjia.com/PHPjc/371720.html www.bkjia.com true http://www.bkjia.com/PHPjc/371720.html techarticle In general, we can direct the user to download a file by directly directing the URL to a file located under document root. However, to do so, there is no way to do some statistics, permissions ...

  • Related Article

    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.