当前位置:Gxlcms > PHP教程 > php强制下载文件的代码(IE浏览器)

php强制下载文件的代码(IE浏览器)

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

  1. //header("Cache-Control: public");
  2. header('content-type:application/vnd.ms-excel');
  3. header("Content-Disposition:attachment; filename=report.xls");

如果不加第一句,会弹出 : Internet Explorer 无法下载 **.php (来自**网站)。Internet Explorer无法打开该 internet 网站。请求的网站不可用,或找不到,请以后再试。

而且连名字都不是所设的名字:report.xls,而是 **.php,把第一句加上就可以了。

在看 rar,gif 之类的,不加第一句,居然通过,不弹出那个错误框框!

如果是 gif等图片的话 ,Content-Disposition:attachment; 会强制弹出一个保存对话框。如果省略或是 inline 就会直接在网页里显示。

Content-type 应取值,如下:

  1. switch( $file_extension ) {

  2. case "pdf": $ctype="application/pdf"; break;
  3. case "exe": $ctype="application/octet-stream"; break;
  4. case "zip": $ctype="application/zip"; break;
  5. case "doc": $ctype="application/msword"; break;
  6. case "xls": $ctype="application/vnd.ms-excel"; break;
  7. case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  8. case "gif": $ctype="image/gif"; break;
  9. case "png": $ctype="image/png"; break;
  10. case "jpeg":
  11. case "jpg": $ctype="image/jpg"; break;
  12. case "mp3": $ctype="audio/mpeg"; break;
  13. case "wav": $ctype="audio/x-wav"; break;
  14. case "mpeg":
  15. case "mpg":
  16. case "mpe": $ctype="video/mpeg"; break;
  17. case "mov": $ctype="video/quicktime"; break;
  18. case "avi": $ctype="video/x-msvideo"; break;

  19. //The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)

  20. case "php":
  21. case "htm":
  22. case "html":
  23. case "txt": die("Cannot be used for ". $file_extension ." files!"); break;

  24. default: $ctype="application/force-download";

  25. }

人气教程排行