Apache Mod_xsendfile How to increase php file download speed

Source: Internet
Author: User
Tags readfile

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

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.