当前位置:Gxlcms > php框架 > 兼容ie6浏览器的php下载文件代码分享

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

时间:2021-07-01 10:21:17 帮助过:40人阅读

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

  1. $filename = "./".$_REQUEST['name']; //这里是枫哥具体路径,只要$filename是所要下载文件的完整路径就可以.
  2. if (!is_file($filename)) {//检测文件是否存在.
  3. die('下载的文件好像跟TM谁私奔啦!');
  4. }
  5. $filepath = str_replace('\\', '/', realpath($filename));
  6. $filesize = filesize($filepath);
  7. $filename = substr(strrchr('/'.$filepath, '/'), 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. }
  22. else
  23. {
  24. header('Pragma: no-cache');
  25. }
  26. $handle = fopen($filepath, 'rb');
  27. fpassthru($handle);
  28. fclose($handle);

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

人气教程排行