PHP Download the same file in different names (X-sendfile) "Turn"

Source: Internet
Author: User
Tags php download sendfile

1, Linux under Nginx default support X-sendfile mode

Nginx supports this feature by default and does not require additional modules to be loaded. The HTTP header that needs to be sent is x-accel-redirect. In addition, the following settings need to be made in the configuration file

location/protected/{internal; Root/some/path; }

Internal indicates that this path can only be accessed within Nginx, and cannot be accessed directly from the browser to prevent unauthorized downloads.

PHP sends X-accel-redirect to Nginx:

<?php $filePath = '/protected/iso.img '; Header (' Content-type:application/octet-stream '); Header (' content-disposition:attachment; Filename= '. basename ($file). ‘"‘); Let xsendfile send the file header (' X-accel-redirect: '. $filePath);?>

The user will then be downloaded to the file under the/some/path/protected/iso.img path.

If you want to send a/some/path/iso.img file, then the Nginx configuration should be

location/protected/{internal; alias/some/path/; # Note the last slash

}

2, Apache under Windows, need to load module mod_xsendfile

Downloaded from https://tn123.org/mod_xsendfile/ Win32 binaries , placed in Apache's modules directory, configured in http.conf

LoadModule xsendfile_module modules/mod_xsendfile.so Xsendfile on

<?php $file = "/tmp/dummy.tar.gz";//To use absolute path, Windows such as D:\tmp\ header ("Content-type:application/octet-stream"); Header (' content-disposition:attachment; Filename= '. basename ($file). ‘"‘); Header ("X-sendfile:". $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:

<?php $file = "/tmp/Chinese name. tar.gz";
$filename = basename ($file);
Header ("Content-type:application/octet-stream");
Handle the Chinese file name if (Strpos ($_server["Http_user_agent"], "MSIE") = = = False) header (' content-disposition:attachment; filename = "'. ($name). ‘"‘); else header (' content-disposition:attachment; Filename= '. Rawurlencode ($name). ‘"‘);
Let xsendfile send the file header ("X-sendfile: $file");

The X-sendfile header is processed by Apache and the response file is sent directly to the client.

Original address: http://blog.csdn.net/ownfire/article/details/11522213

PHP Download the same file in different names (X-sendfile) "Turn"

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.