The website downloads the file is a frequently used function, but looks very simple, does the time various problems appear, the heart collapses has not ...
No, let's get to the point.
Often online answers are the same:
Source file path
$thefile = ' test.text ';
File download
$fileinfo = PathInfo ($filename);
Header (' content-type:application/x-'. $fileinfo [' extension ']);
Header (' Content-disposition:attachment filename= '. $fileinfo [' basename ']);
Header (' Content-length: ' FileSize ($filename));
ReadFile ($thefile);
Exit ();
or this.
Source file path
$thefile = ' test.text ';
$filename = $thefile;
$file = fopen ($filename, "RB");
Header ("Content-type: application/octet-stream");
Header ("accept-ranges: bytes");
Header ("Content-disposition: attachment; Filename= 4.doc ");
$contents = "";
while (!feof ($file)) {
$contents. = Fread ($file, 8192);
}
Fclose ($file);
The above download small file completely no problem, but I want to download large files, large files, large files, you will paste show me this ...
So I gave up a certain degree, with Google.
1. First open php.ini Find memory_limit modify you want to occupy memory, I have a large server memory so high, see the situation adjustment.
Memory_limit = 1024M
Okay, let's try downloading the big files.
$file = fopen ($file _dir, "R"); Enter the file label Header ("Content-type:application/octet-stream"); Header ("Accept-ranges:bytes"); Header ("Accept-length:"). FileSize ($file _dir)); Header ("content-disposition:attachment; Filename= ". $attach [' name ']); Output file content echo fread ($file, FileSize ($file _dir)); Fclose ($file); Exit ();
Really can download hundreds of trillion files, so happy. But how can I not open the site? What What the heck. You play me. The edge of the crash
All right. I'll see what the problem is. Go ahead, Google, okay. Problem is
This is a way to download files, but it consumes a lot of resources, and it takes a long time to consume server-side script resources and servers ' memory resources.
php function download file to occupy the PHP memory, how to solve.
Of course continue to Google. Universal Google and let me see the hope.
Access to information, we can use the X-sendfile module to achieve more efficient file downloads.
X-sendfile is a modern operating system supported by a high-performance network IO, server-side scripting such as PHP is responsible for building the request header information, and then download the time does not require PHP to participate in the Web servers directly processing X-sendfile header information, and sends the response file directly to the browser client, which avoids memory footprint.
First we want to look at Apache version information: Phpinfo ();
then download the corresponding X-sendfile module package (download address: https://github.com/nmaier/mod_xsendfile).
Linux has a faster installation method: Reference address: http://blogs.isb.bj.edu.cn/miles/2014/02/20/installing-mod_xsendfile-on-centos-6/
Yum Install Mod_xsendfile
Linux: Mine is CentOS.
1. Open the following road strength, there is a xsendfile.conf (if you install the MoD after successful)
CD/ETC/HTTPD/CONF.MODULES.D
2.VI Open xsendfile.conf, add the following two lines, save exit (: Wq), restart httpd (systemctl restart httpd)
VI xsendfile.conf
LoadModule xsendfile_module modules/mod_xsendfile.so
xsendfile on
xsendfilepath/var/www
windows:
1, will extract the mod_xsendfile.so file copy to the Apache modules directory, must choose the corresponding version 24 is 2.4,22 is 2.2
2, configure the Apache directory of the httpd.conf file, add the following code:
LoadModule xsendfile_module modules/mod_xsendfile.so
xsendfile
on Xsendfilepath f:/Web site Lu Jing (or your file directory)
3, restart the Apache service, restart successful.
After the work is done, we can use X-sendfile to implement the file download:
$ua = $_server["Http_user_agent"];
$filename = $attach [' name '];
$encoded _filename = UrlEncode ($filename);
$encoded _filename = str_replace ("+", "%20", $encoded _filename);
Input file label
$realpath = Realpath ($file _dir);
Header (' Content-type:application/octet-stream ');
Handle Chinese filename, avoid file name garbled
$encoded _filename = Rawurlencode ($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. '"');
}
Let xsendfile send file
header ("X-sendfile: $realpath");
Exit ();
Good excited hurriedly try, download big file No problem, the website can be normal access, want is this effect, quack quack.
Spit: A lot of is copied and pasted a word not many words, a hair, the key is that he has not tried this code is no use, I really do not know what it means