当前位置: 移动技术网 > IT编程>开发语言>PHP > 兼容ie6浏览器的php下载文件代码分享

兼容ie6浏览器的php下载文件代码分享

2019年03月21日  | 移动技术网IT编程  | 我要评论

ps:这段代码在win系统下跑没有问题,枫哥亲测可用.如果用的是linux或是其他系统,要要诸位自行测试下啦.

$filename = "./".$_request['name']; //这里是枫哥具体路径,只要$filename是所要下载文件的完整路径就可以.
if (!is_file($filename)) {//检测文件是否存在.
 die('下载的文件好像跟tm谁私奔啦!');
}
$filepath = str_replace('\\', '/', realpath($filename));
$filesize = filesize($filepath);
$filename = substr(strrchr('/'.$filepath, '/'), 1);
$extension = strtolower(substr(strrchr($filepath, '.'), 1));
       // use this unless you want to find the mime type based on extension,文件后缀格式,不解释.
$mime = array('application/octet-stream');
header('content-type: '.$mime);
header('content-disposition: attachment; filename="'.$filename.'"');
header('content-transfer-encoding: binary');
header('content-length: '.sprintf('%d', $filesize));
header('expires: 0');
// check for ie only headers,坑爹的ie检测,你懂的.
if (isset($_server['http_user_agent']) &&((strpos($_server['http_user_agent'], 'msie') !== false)))
{
 header('cache-control: must-revalidate, post-check=0, pre-check=0');
 header('pragma: public');
}
else
{
 header('pragma: no-cache');
}
$handle = fopen($filepath, 'rb');
fpassthru($handle);
fclose($handle);

这个代码是枫哥从国外的一个站点上找到的,亲测可用.度娘出来的结果,你懂的.因此在博客上共享出来,希望对大家有用.国外的站,地址忘了,没有办法放出,理解下.最后,严肃的声明下:这个代码不是我写的,是转载的.

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网