文件下载后,站点无响应。我做错了什么? [英] Site gets unresponsive after file download. What do I do wrong?

查看:143
本文介绍了文件下载后,站点无响应。我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,我用来从硬盘下载一个文件。我得到的问题是,如果我在中间停止下载或下载结束后,该网站将无响应。任何想法为什么?环境是LAMP。

 


// 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆