|
把下面的函数封装在CI的自定义library中,再通过controller调用的。
浏览器在等待这次文件下载完成之后,才会去响应其他请求
其他请求貌似被阻塞掉了!这难道是CI本身的原因?有什么办法解决吗?
下面是文件下载的代码:
PHP复制代码
function file_down ($url,$filename)
{
$CI = & get_instance ();
$CI->load->helper('file');
$filename = str_replace(' ', '_', $filename);//存在空格则转化成下划线
$size = filesize($url);
$size2 = $size - 1;
$range = 0;
if(isset($_SERVER['HTTP_RANGE']))
{
header("HTTP/1.1 206 Partial Content");
$range = str_replace("=", "-", $_SERVER['HTTP_RANGE']);
$range = explode("-", $range);
$range = trim($range[1]);
$length = $size2-$range;
header("Content-Length: ".$length);
header("Content-Range: bytes " .$range."-" .$size2 ."/".$size);
}
else
{
header("Content-Length:".$size);
header("Content-Range:bytes 0-$size2/$size");
}
@header("Cache-control: private");
header("Accept-Ranges: bytes");
$mime = get_mime_by_extension ($url); //获取文件MIME类型
if(!$mime)
{
$mime="application/octet-stream";
}
header("Content-Type: $mime");
$encode_name = urlencode($filename);
$encode_name = str_replace("+","%20",$encode_name);
$ua = $_SERVER["HTTP_USER_AGENT"];
if(preg_match("/MSIE/",$ua))
{
header("Content-Disposition:attachment;filename=".$encode_name);
}
else if(preg_match("/Firefox/",$ua))
{
header('Content-Disposition:attachment;filename*="utf8\'\''.$filename.'"');
}
else
{
header("Content-Disposition:attachment;filename=".$filename);
}
$fp = @fopen($url, "rb");
fseek($fp,$range);
while(!feof($fp))
{
set_time_limit(0);
print(fread($fp,1024));
@flush();
@ob_flush();
}
fclose($fp);
exit();
}
复制代码 |
|