Download any format file

Source: Internet
Author: User
Support for file download in any format
The function has two parameters, the first parameter is the file completes the path in the server, the second parameter is the download display file name.
  1. /**
  2. * Download File
  3. * filename does not include suffix name
  4. */
  5. Public function Download ($_path, $filename = ") {
  6. if (file_exists ($_path)) {
  7. $fullPath = CHtml::d ecode ($_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 (' file does not exist! ', 1);
  76. }
  77. }
Copy Code
  • 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.