当前位置:Gxlcms > PHP教程 > php下载文件代码(兼容ie6浏览器)

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

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

  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. } // bbs.it-home.org
  22. else
  23. {
  24. header('Pragma: no-cache');
  25. }
  26. $handle = fopen($filepath, 'rb');
  27. fpassthru($handle);
  28. fclose($handle);

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

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

人气教程排行