任意格式檔案下載

來源:互聯網
上載者:User
支援任意格式的檔案下載
函數有兩個參數,第一個參數是檔案在伺服器中完成路徑,第二個參數是下載顯示檔案名稱。
  1. /**
  2. * 下載檔案
  3. * filename 不包括尾碼名
  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('檔案不存在!', 1);
  76. }
  77. }
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.