PHP Mandatory download file code (ie browser)

Source: Internet
Author: User
    1. Header ("Cache-control:public");
    2. Header (' Content-type:application/ ');
    3. Header ("content-disposition:attachment; Filename=report.xls ");
Copy Code

If you don't add the first sentence, it pops up: Internet Explorer cannot download **.php (from * * website). Internet Explorer cannot open the Internet site. The requested Web site is not available, or cannot be found, please try again later.

And even the name is not the name of the set: Report.xls, but **.php, the first sentence to add on it.

Look at Rar,gif and the like, do not add the first sentence, incredibly through, do not pop the error box!

If it is GIF and other pictures, content-disposition:attachment; A Save dialog box is forced to pop up. If omitted or inline, it will be displayed directly on the page.

The content-type should be evaluated 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/"; Break
  7. Case "ppt": $ctype = "Application/"; 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 is for extensions that shouldn ' t is downloaded (sensitive stuff, like PHP files)

  20. Case "PHP":
  21. Case "htm":
  22. Case "HTML":
  23. Case ' txt ': Die ("cannot is used for". $file _extension. "Files!"); Break

  24. Default: $ctype = "Application/force-download";

  25. }

Copy Code
  • Related Article

    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.