欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-03-09 18:15:14
...
  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);
复制代码

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

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