Description
A file download is provided in the Apache server, and a URL is typically used to point to a file on the server for download.
Disadvantages: Can not be statistics, permission detection and other operations.
1, generally use PHP to provide download, for example:
Copy Codecode example: <?php
$file = ' Test.zip ';
if (file_exists ($file)) {
Header (' Content-type:application/octet-stream ');
Header (' content-disposition:attachment; Filename= '. basename ($file));
Header (' Content-length: '. FileSize ($file));
ReadFile ($file);
}
?>
2, processing Chinese file name:
Copy Codecode example: <?php
$file = ' Test.zip ';
$filename = ' Chinese. zip ';
if (file_exists ($file)) {
$user _agent = $_server[' http_user_agent ');
$encode _filename = Rawurlencode ($filename);
if (Preg_match ("/msie/", $user _agent)) {
Header (' content-disposition:attachment; filename= '. $encode _filename. ');
}else if (Preg_match ("/firefox/", $user _agent)) {
Header ("content-disposition:attachment; Filename*=\ "UTF8". $filename. ' "');
}else{
Header (' content-disposition:attachment; Filename= '. $filename. ');
}
ReadFile ($file);
}
?>
Using PHP ReadFile, you need to go through this layer of PHP.
If the file can be sent directly to the user through Apache, without the PHP layer, it will increase the download speed.
Then need to use the main character of this article, it is the Apache mod_xsendfile module,: Mod_xsendfile (https://tn123.org/mod_xsendfile/), let Apache directly send the file to the user
1. Install the Mod_xsendfile module:
Copy Codecode example: sudo apxs2-cia mod_xsendfile.c
sudo a2enmod xsendfile
Sudo/etc/init.d/apache2 restart
APXS2 is used to compile Apache module and needs to be installed Apache2-dev
2, set Xsendfile open:
Copy CodeCode Sample:<directory>
Xsendfile on
</Directory>
The instance code of the 3,mod_xsendfile module.
Copy Codecode example: <?php
/**
* Mod_xsendfile module Acceleration File download
* edit:www.jbxue.com
*/
$file = ' Test.zip ';
$filename = ' Chinese. zip ';
if (file_exists ($file)) {
$user _agent = $_server[' http_user_agent ');
$encode _filename = Rawurlencode ($filename);
if (Preg_match ("/msie/", $user _agent)) {
Header (' content-disposition:attachment; filename= '. $encode _filename. ');
}else if (Preg_match ("/firefox/", $user _agent)) {
Header ("content-disposition:attachment; Filename*=\ "UTF8". $filename. ' "');
}else{
Header (' content-disposition:attachment; Filename= '. $filename. ');
}
Header (' X-sendfile: '. $file);
}
?>
Apache Mod_xsendfile How to increase php file download speed