文件下载后,站点无响应。我做错了什么? [英] Site gets unresponsive after file download. What do I do wrong?
问题描述
。
。
。
// get mime type
if($ dwld_allowed_ext [$ fext] ==''){
$ mtype ='';
// mime类型未设置,从服务器设置获取
if($ file_type == null){
if(function_exists('mime_content_type')){
$ mtype = mime_content_type($ FILE_PATH);
}
else if(function_exists('finfo_file')){
$ finfo = finfo_open(FILEINFO_MIME); // return mime type
$ mtype = finfo_file($ finfo,$ file_path);
finfo_close($ finfo);
}
}
else {
$ mtype = $ file_type;
}
if($ mtype ==''){
$ mtype =application / force-download;
}
}
else {
//获取由admin
$ mtype = $ dwld_allowed_ext定义的MIME类型[$ fext];
}
$ asfname = $ fname;
// set headers
header(Pragma:public);
header(Expires:0);
header(Cache-Control:must-revalidate,post-check = 0,pre-check = 0);
header(Cache-Control:public);
header(Content-Description:File Transfer);
header(Content-Type:$ mtype);
header(Content-Disposition:attachment; filename = \$ asfname\);
header(Content-Transfer-Encoding:binary);
header(Content-Length:。$ fsize);
//下载
// @readfile($ file_path);
$ file = @fopen($ file_path,rb);
if($ file){
while(!feof($ file)){
print(fread($ file,1024 * 8));
flush();
if(connection_status()!= 0){
@fclose($ file);
die();
}
}
@fclose($ file);
}
调用 session_write_close()发送任何下载标题之前。
请参阅此评论:
可能值得注意的是,如果您的
网站使用前端控制器
会话,并发送一个大文件
a用户; 您应该在发送文件之前结束会话
否则用户将无法
继续浏览网站
,而文件正在下载。
I have the code below that i use to download a file from the HDD. The problem I get is that if I stop a download in the middle or after the download ends, the site becomes unresponsive. Any idea why? The environment is LAMP.
.
.
.
// get mime type
if ($dwld_allowed_ext[$fext] == '') {
$mtype = '';
// mime type is not set, get from server settings
if ($file_type == null) {
if (function_exists('mime_content_type')) {
$mtype = mime_content_type($file_path);
}
else if (function_exists('finfo_file')) {
$finfo = finfo_open(FILEINFO_MIME); // return mime type
$mtype = finfo_file($finfo, $file_path);
finfo_close($finfo);
}
}
else {
$mtype = $file_type;
}
if ($mtype == '') {
$mtype = "application/force-download";
}
}
else {
// get mime type defined by admin
$mtype = $dwld_allowed_ext[$fext];
}
$asfname = $fname;
// set headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mtype");
header("Content-Disposition: attachment; filename=\"$asfname\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);
// download
// @readfile($file_path);
$file = @fopen($file_path,"rb");
if ($file) {
while(!feof($file)) {
print(fread($file, 1024*8));
flush();
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
Call session_write_close() right before sending any download headers.
See this comment:
It might be worth noting that if your site uses a front controller with sessions and you send a large file to a user; you should end the session just before sending the file, otherwise the user will not be able to continue continue browsing the site while the file is downloading.
这篇关于文件下载后,站点无响应。我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!