Home > Backend Development > PHP Tutorial > Download files in any format

Download files in any format

WBOY
Release: 2016-07-25 08:46:01
Original
928 people have browsed it
Supports downloading files in any format
The function has two parameters. The first parameter is the path of the file in the server, and the second parameter is the name of the downloaded file.
  1. /**
  2. * Download file
  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('File does not exist! ', 1);
  76. }
  77. }
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template