php下载文件代码(兼容ie6浏览器)

原创
2016-07-25 08:53:03 709浏览
  1. $filename = "./".$_REQUEST['name']; //具体路径,只要$filename是所要下载文件的完整路径即可
  2. if (!is_file($filename)) {//检测文件是否存在.
  3. die('下载的文件好像跟TM谁私奔啦!');
  4. }
  5. $filepath = str_replace('\\', '//m.sbmmt.com/m/', realpath($filename));
  6. $filesize = filesize($filepath);
  7. $filename = substr(strrchr('//m.sbmmt.com/m/'.$filepath, '//m.sbmmt.com/m/'), 1);
  8. $extension = strtolower(substr(strrchr($filepath, '.'), 1));
  9. // use this unless you want to find the mime type based on extension,文件后缀格式,不解释.
  10. $mime = array('application/octet-stream');
  11. header('Content-Type: '.$mime);
  12. header('Content-Disposition: attachment; filename="'.$filename.'"');
  13. header('Content-Transfer-Encoding: binary');
  14. header('Content-Length: '.sprintf('%d', $filesize));
  15. header('Expires: 0');
  16. // check for IE only headers,坑爹的IE检测,你懂的.
  17. if (isset($_SERVER['HTTP_USER_AGENT']) &&((strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)))
  18. {
  19. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  20. header('Pragma: public');
  21. } // bbs.it-home.org
  22. else
  23. {
  24. header('Pragma: no-cache');
  25. }
  26. $handle = fopen($filepath, 'rb');
  27. fpassthru($handle);
  28. fclose($handle);
复制代码

说明: 以上代码是枫哥从国外的一个站点上找到的,亲测可用.

脚本小编收集整理,感谢原作者枫哥出品~~~



声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:phpexcel读取excel文件的方法 下一条:php导出excel单元格自动换行怎么实现?

相关文章

查看更多