PHP implementation of File download (multiple file format) code

Source: Internet
Author: User
  1. /**

  2. * File Download
  3. * Multiple file formats, including PDF, zip, gif, JPG, MPEG, Word, etc.
  4. * Edit bbs.it-home.org
  5. */
  6. function Dl_file ($file) {
  7. First, see if the file exists
  8. if (!is_file ($file)) {die ("404 File Not found!");}

  9. Gather relevent info about file

  10. $len = FileSize ($file);
  11. $filename = basename ($file);
  12. $file _extension = Strtolower (substr (STRRCHR ($filename, "."), 1);

  13. This would set the Content-type to the appropriate setting for the file

  14. Switch ($file _extension) {
  15. Case "PDF": $ctype = "Application/pdf"; Break
  16. Case "EXE": $ctype = "Application/octet-stream"; Break
  17. Case "Zip": $ctype = "Application/zip"; Break
  18. Case "Doc": $ctype = "Application/msword"; Break
  19. Case "xls": $ctype = "application/vnd.ms-excel"; Break
  20. Case "ppt": $ctype = "Application/vnd.ms-powerpoint"; Break
  21. Case "gif": $ctype = "Image/gif"; Break
  22. Case "png": $ctype = "Image/png"; Break
  23. Case "JPEG":
  24. Case "jpg": $ctype = "image/jpg"; Break
  25. Case "MP3": $ctype = "Audio/mpeg"; Break
  26. Case "wav": $ctype = "Audio/x-wav"; Break
  27. Case "MPEG":
  28. Case "MPG":
  29. Case "MPE": $ctype = "Video/mpeg"; Break
  30. Case "mov": $ctype = "Video/quicktime"; Break
  31. Case "avi": $ctype = "Video/x-msvideo"; Break

  32. The following is for extensions that shouldn ' t is downloaded (sensitive stuff, like PHP files)

  33. Case "PHP":
  34. Case "htm":
  35. Case "HTML":
  36. Case ' txt ': Die ("cannot is used for". $file _extension. "Files!"); Break

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

  38. }

  39. Begin Writing Headers

  40. Header ("Pragma:public");
  41. Header ("expires:0");
  42. Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
  43. Header ("Cache-control:public");
  44. Header ("Content-description:file Transfer");
  45. Use the switch-generated content-type
  46. Header ("Content-type: $ctype");

  47. Force the Download

  48. $header = "content-disposition:attachment; Filename= ". $filename."; ";
  49. Header ($header);
  50. Header ("Content-transfer-encoding:binary");
  51. Header ("Content-length:". $len);
  52. @readfile ($file);
  53. Exit
  54. }
  55. ?>

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: info-contact@alibabacloud.com 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.