Php code for Force File Download (ie browser)

Source: Internet
Author: User
Php code for Force File Download (ie browser)

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

If you do not add the first sentence, Internet Explorer cannot download **. php (from The ** website ). Internet Explorer cannot open this internet website. The requested website is unavailable or cannot be found. please try again later.

In addition, the name is not the set name: report.xls, But **. php. you can add the first sentence.

When reading rar, gif, and so on, the first sentence is not added. the error box is not displayed!

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

The value of Content-type is 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. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.