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"