Download files in any format

Source: Internet
Author: User
Download files in any format
Supports file downloads in any format
The function has two parameters. The first parameter is the file completion path on the server, and the second parameter is the download display file name.

  1. /**
  2. * Download an object
  3. * Filename does not include the suffix
  4. */
  5. Public function download ($ _ path, $ filename = ''){
  6. If (file_exists ($ _ path )){
  7. $ FullPath = CHtml: decode ($ _ path );
  8. $ Filename = $ filename? $ Filename: substr (strrchr ($ fullPath, '/'), 1 );
  9. // Parse Info/Get Extension
  10. $ Fsize = filesize ($ fullPath );
  11. $ Path_parts = pathinfo ($ fullPath );
  12. $ Ext = strtolower ($ path_parts ["extension"]);
  13. $ Filename. = '.'. $ ext;
  14. // Determine Content Type
  15. Switch ($ ext ){
  16. Case 'apk ':
  17. $ Ctype = 'application/vnd. android. package-archive ';
  18. Break;
  19. Case 'chm ':
  20. $ Ctype = 'application/octet-stream ';
  21. Break;
  22. Case "pdf ":
  23. $ Ctype = "application/pdf ";
  24. Break;
  25. Case "txt ":
  26. $ Ctype = "application/txt ";
  27. Break;
  28. Case "zip ":
  29. $ Ctype = "application/zip ";
  30. Break;
  31. Case "doc ":
  32. $ Ctype = "application/msword ";
  33. Break;
  34. Case "xls ":
  35. $ Ctype = "application/vnd. ms-excel ";
  36. Break;
  37. Case "ppt ":
  38. $ Ctype = "application/vnd. ms-powerpoint ";
  39. Break;
  40. Case "gif ":
  41. $ Ctype = "image/gif ";
  42. Break;
  43. Case "png ":
  44. $ Ctype = "image/png ";
  45. Break;
  46. Case "jpeg ":
  47. Case "jpg ":
  48. $ Ctype = "image/jpg ";
  49. Break;
  50. Default:
  51. $ Ctype = "application/force-download ";
  52. }
  53. $ Ua = $ _ SERVER ["HTTP_USER_AGENT"];
  54. $ Encoded_filename = rawurlencode ($ filename );
  55. $ Encoded_filename = str_replace ("+", "% 20", $ encoded_filename );
  56. Header ("Pragma: public"); // required
  57. Header ("Expires: 0 ");
  58. Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
  59. Header ("Cache-Control: private", false); // required for certain browsers
  60. Header ("Content-Type: $ ctype ");
  61. // Header ('content-Disposition: attachment; filename = "'. rawurlencode ($ filename ).'"');
  62. If (preg_match ("/MSIE/", $ ua )){
  63. Header ('content-Disposition: attachment; filename = "'. $ encoded_filename .'"');
  64. } Else if (preg_match ("/Firefox/", $ ua )){
  65. Header ("Content-Disposition: attachment; filename * = utf8''". $ filename .'"');
  66. } Else {
  67. Header ('content-Disposition: attachment; filename = "'. $ filename .'"');
  68. }
  69. Header ("Content-Transfer-Encoding: binary ");
  70. Header ("Content-Length:". $ fsize );
  71. Ob_clean ();
  72. Flush ();
  73. Readfile ($ fullPath );
  74. } Else {
  75. Throw new Exception ('The file does not exist! ', 1 );
  76. }
  77. }

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.