获取文件mime类型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取文件mime类型相关的知识,希望对你有一定的参考价值。

You only use the filename as $value
for get_mimetype().
  1. function get_file_extension($file) {
  2.  
  3. return array_pop(explode('.',$file));
  4. }
  5.  
  6. function get_mimetype($value='') {
  7.  
  8. $ct['htm'] = 'text/html';
  9. $ct['html'] = 'text/html';
  10. $ct['txt'] = 'text/plain';
  11. $ct['asc'] = 'text/plain';
  12. $ct['bmp'] = 'image/bmp';
  13. $ct['gif'] = 'image/gif';
  14. $ct['jpeg'] = 'image/jpeg';
  15. $ct['jpg'] = 'image/jpeg';
  16. $ct['jpe'] = 'image/jpeg';
  17. $ct['png'] = 'image/png';
  18. $ct['ico'] = 'image/vnd.microsoft.icon';
  19. $ct['mpeg'] = 'video/mpeg';
  20. $ct['mpg'] = 'video/mpeg';
  21. $ct['mpe'] = 'video/mpeg';
  22. $ct['qt'] = 'video/quicktime';
  23. $ct['mov'] = 'video/quicktime';
  24. $ct['avi'] = 'video/x-msvideo';
  25. $ct['wmv'] = 'video/x-ms-wmv';
  26. $ct['mp2'] = 'audio/mpeg';
  27. $ct['mp3'] = 'audio/mpeg';
  28. $ct['rm'] = 'audio/x-pn-realaudio';
  29. $ct['ram'] = 'audio/x-pn-realaudio';
  30. $ct['rpm'] = 'audio/x-pn-realaudio-plugin';
  31. $ct['ra'] = 'audio/x-realaudio';
  32. $ct['wav'] = 'audio/x-wav';
  33. $ct['css'] = 'text/css';
  34. $ct['zip'] = 'application/zip';
  35. $ct['pdf'] = 'application/pdf';
  36. $ct['doc'] = 'application/msword';
  37. $ct['bin'] = 'application/octet-stream';
  38. $ct['exe'] = 'application/octet-stream';
  39. $ct['class']= 'application/octet-stream';
  40. $ct['dll'] = 'application/octet-stream';
  41. $ct['xls'] = 'application/vnd.ms-excel';
  42. $ct['ppt'] = 'application/vnd.ms-powerpoint';
  43. $ct['wbxml']= 'application/vnd.wap.wbxml';
  44. $ct['wmlc'] = 'application/vnd.wap.wmlc';
  45. $ct['wmlsc']= 'application/vnd.wap.wmlscriptc';
  46. $ct['dvi'] = 'application/x-dvi';
  47. $ct['spl'] = 'application/x-futuresplash';
  48. $ct['gtar'] = 'application/x-gtar';
  49. $ct['gzip'] = 'application/x-gzip';
  50. $ct['js'] = 'application/x-javascript';
  51. $ct['swf'] = 'application/x-shockwave-flash';
  52. $ct['tar'] = 'application/x-tar';
  53. $ct['xhtml']= 'application/xhtml+xml';
  54. $ct['au'] = 'audio/basic';
  55. $ct['snd'] = 'audio/basic';
  56. $ct['midi'] = 'audio/midi';
  57. $ct['mid'] = 'audio/midi';
  58. $ct['m3u'] = 'audio/x-mpegurl';
  59. $ct['tiff'] = 'image/tiff';
  60. $ct['tif'] = 'image/tiff';
  61. $ct['rtf'] = 'text/rtf';
  62. $ct['wml'] = 'text/vnd.wap.wml';
  63. $ct['wmls'] = 'text/vnd.wap.wmlscript';
  64. $ct['xsl'] = 'text/xml';
  65. $ct['xml'] = 'text/xml';
  66.  
  67. $extension = get_file_extension($value);
  68.  
  69. if (!$type = $ct[strtolower($extension)]) {
  70.  
  71. $type = 'text/html';
  72. }
  73.  
  74. return $type;
  75. }

以上是关于获取文件mime类型的主要内容,如果未能解决你的问题,请参考以下文章

无法获取 .flv 文件的正确 mime 类型

从 C 中的文件名获取 MIME 类型

如何在 Qt 中获取文件的 MIME 类型?

Java 依据文件名后缀,获取Content-Type/Mime类型

Laravel 4 中 File::mime() 的替换(从文件扩展名中获取 mime 类型)

使用 fileinfo 获取 mime 类型,是大文件的好方法吗?