Home  >  Article  >  Backend Development  >  PHP code to force download files (IE browser)

PHP code to force download files (IE browser)

WBOY
WBOYOriginal
2016-07-25 08:54:461120browse
  1. //header("Cache-Control: public");
  2. header('content-type:application/vnd.ms-excel');
  3. header("Content-Disposition:attachment ; filename=report.xls");
Copy the code

If you don’t add the first sentence, it will pop up: Internet Explorer cannot download **.php (from ** website). Internet Explorer cannot open the internet website. The requested website is unavailable or cannot be found. Please try again later.

And even the name is not the set name: report.xls, but **.php, just add the first sentence.

When I was looking at rar, gif and the like, without adding the first sentence, it passed without the error box popping up!

If it is a picture such as gif, Content-Disposition:attachment; will force a save dialog box to pop up. If omitted or inline, it will be displayed directly on the web page.

Content-type should take a value as follows:

  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. }
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn