php获取文件mime类型程序代码

在php中获取文件的mime类型方法有很多种,我们来介绍直接利用mime_content_type()函数判断获取mime类型即可了.

mime_content_type返回指定文件的MIME类型,用法,代码如下:

  1. echo mime_content_type ( 'php.gif' ) . "n" ;
  2. echo mime_content_type ( 'test.php' );

输出:image/gif,text/plain,但是此函数在php5.3.0以后就不可用了,如果你是php5.3.0以后版本我们可使用如下代码来操作:

  1. $finfo = finfo_open(FILEINFO_MIME);
  2. $mimetype = finfo_file($finfo, $filename);
  3. finfo_close($finfo);

下面介绍用户自定的我这个是没有php版本限制,代码如下:

  1. <?php
  2. $mime = array (
  3. //applications
  4. 'ai' => 'application/postscript',
  5. 'eps' => 'application/postscript',
  6. 'exe' => 'application/octet-stream',
  7. 'doc' => 'application/vnd.ms-word',
  8. 'xls' => 'application/vnd.ms-excel',
  9. 'ppt' => 'application/vnd.ms-powerpoint',
  10. 'pps' => 'application/vnd.ms-powerpoint',
  11. 'pdf' => 'application/pdf',
  12. 'xml' => 'application/xml',
  13. 'odt' => 'application/vnd.oasis.opendocument.text',
  14. 'swf' => 'application/x-shockwave-flash',
  15. // archives
  16. 'gz' => 'application/x-gzip',
  17. 'tgz' => 'application/x-gzip',
  18. 'bz' => 'application/x-bzip2',
  19. 'bz2' => 'application/x-bzip2',
  20. 'tbz' => 'application/x-bzip2',
  21. 'zip' => 'application/zip',
  22. 'rar' => 'application/x-rar',
  23. 'tar' => 'application/x-tar',
  24. '7z' => 'application/x-7z-compressed',
  25. // texts
  26. 'txt' => 'text/plain',
  27. 'php' => 'text/x-php',
  28. 'html' => 'text/html',
  29. 'htm' => 'text/html',
  30. 'js' => 'text/javascript',
  31. 'css' => 'text/css',
  32. 'rtf' => 'text/rtf',
  33. 'rtfd' => 'text/rtfd',
  34. 'py' => 'text/x-python',
  35. 'java' => 'text/x-java-source',
  36. 'rb' => 'text/x-ruby',
  37. 'sh' => 'text/x-shellscript',
  38. 'pl' => 'text/x-perl',
  39. 'sql' => 'text/x-sql',
  40. // images
  41. 'bmp' => 'image/x-ms-bmp',
  42. 'jpg' => 'image/jpeg',
  43. 'jpeg' => 'image/jpeg',
  44. 'gif' => 'image/gif',
  45. 'png' => 'image/png',
  46. 'tif' => 'image/tiff',
  47. 'tiff' => 'image/tiff',
  48. 'tga' => 'image/x-targa',
  49. 'psd' => 'image/vnd.adobe.photoshop',
  50. //audio
  51. 'mp3' => 'audio/mpeg',
  52. 'mid' => 'audio/midi',
  53. 'ogg' => 'audio/ogg',
  54. 'mp4a' => 'audio/mp4',
  55. 'wav' => 'audio/wav',
  56. 'wma' => 'audio/x-ms-wma',
  57. // video
  58. 'avi' => 'video/x-msvideo',
  59. 'dv' => 'video/x-dv',
  60. 'mp4' => 'video/mp4',
  61. 'mpeg' => 'video/mpeg',
  62. 'mpg' => 'video/mpeg',
  63. 'mov' => 'video/quicktime',
  64. 'wm' => 'video/x-ms-wmv',
  65. 'flv' => 'video/x-flv',
  66. 'mkv' => 'video/x-matroska'
  67. );
  68. function _getMimeDetect() {
  69. if (class_exists('finfo')) {
  70. return 'finfo';
  71. } else if (function_exists('mime_content_type')) {
  72. return 'mime_content_type';
  73. } else if ( function_exists('exec')) {
  74. $result = exec('file -ib '.escapeshellarg(__FILE__));
  75. if ( 0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) {
  76. return 'linux';
  77. }
  78. $result = exec('file -Ib '.escapeshellarg(__FILE__));
  79. if ( 0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) {
  80. return 'bsd';
  81. }
  82. }
  83. return 'internal';
  84. }
  85. function _getMimeType($path) {
  86. global $mime;
  87. $fmime = _getMimeDetect();
  88. switch($fmime) {
  89. case 'finfo':
  90. $finfo = finfo_open(FILEINFO_MIME);
  91. if ($finfo)
  92. $type = @finfo_file($finfo, $path);
  93. break;
  94. case 'mime_content_type':
  95. $type = mime_content_type($path);
  96. break;
  97. case 'linux':
  98. $type = exec('file -ib '.escapeshellarg($path));
  99. break;
  100. case 'bsd':
  101. $type = exec('file -Ib '.escapeshellarg($path));
  102. break;
  103. default:
  104. $pinfo = pathinfo($path);
  105. $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
  106. $type = isset($mime[$ext]) ? $mime[$ext] : 'unkown';
  107. break;
  108. }
  109. $type = explode(';', $type);
  110. //需要加上这段,因为如果使用mime_content_type函数来获取一个不存在的$path时会返回'application/octet-stream'
  111. if ($fmime != 'internal' AND $type[0] == 'application/octet-stream') {
  112. $pinfo = pathinfo($path);
  113. $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
  114. if (!emptyempty($ext) AND !emptyempty($mime[$ext])) {
  115. $type[0] = $mime[$ext];
  116. }
  117. }
  118. return $type[0];
  119. }
  120. $path = '1.txt'; //实际上当前路径并不存在1.txt
  121. var_dump(_getMimeType($path));
  122. /*End of php*/