php 获取文件MIME类型的实现方法

前端之家收集整理的这篇文章主要介绍了php 获取文件MIME类型的实现方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP获取文件MIME类型,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:
  1. <?PHP
  2. /**
  3. * 获取文件MIME类型
  4. *
  5. * @param
  6. * @author 编程之家 jb51.cc jb51.cc
  7. **/
  8. $mime = array (
  9. //applications
  10. 'ai' => 'application/postscript','eps' => 'application/postscript','exe' => 'application/octet-stream','doc' => 'application/vnd.ms-word','xls' => 'application/vnd.ms-excel','ppt' => 'application/vnd.ms-powerpoint','pps' => 'application/vnd.ms-powerpoint','pdf' => 'application/pdf','xml' => 'application/xml','odt' => 'application/vnd.oasis.opendocument.text','swf' => 'application/x-shockwave-flash',// archives
  11. 'gz' => 'application/x-gzip','tgz' => 'application/x-gzip','bz' => 'application/x-bzip2','bz2' => 'application/x-bzip2','tbz' => 'application/x-bzip2','zip' => 'application/zip','rar' => 'application/x-rar','tar' => 'application/x-tar','7z' => 'application/x-7z-compressed',// texts
  12. 'txt' => 'text/plain','PHP' => 'text/x-PHP','html' => 'text/html','htm' => 'text/html','js' => 'text/javascript','css' => 'text/css','rtf' => 'text/rtf','rtfd' => 'text/rtfd','py' => 'text/x-python','java' => 'text/x-java-source','rb' => 'text/x-ruby','sh' => 'text/x-shellscript','pl' => 'text/x-perl','sql' => 'text/x-sql',// images
  13. 'bmp' => 'image/x-ms-bmp','jpg' => 'image/jpeg','jpeg' => 'image/jpeg','gif' => 'image/gif','png' => 'image/png','tif' => 'image/tiff','tiff' => 'image/tiff','tga' => 'image/x-targa','psd' => 'image/vnd.adobe.photoshop',//audio
  14. 'mp3' => 'audio/mpeg','mid' => 'audio/midi','ogg' => 'audio/ogg','mp4a' => 'audio/mp4','wav' => 'audio/wav','wma' => 'audio/x-ms-wma',// video
  15. 'avi' => 'video/x-msvideo','dv' => 'video/x-dv','mp4' => 'video/mp4','mpeg' => 'video/mpeg','mpg' => 'video/mpeg','mov' => 'video/quicktime','wm' => 'video/x-ms-wmv','flv' => 'video/x-flv','mkv' => 'video/x-matroska'
  16. );
  17. function _getMimeDetect() {
  18. if (class_exists('finfo')) {
  19. return 'finfo';
  20. } else if (function_exists('mime_content_type')) {
  21. return 'mime_content_type';
  22. } else if ( function_exists('exec')) {
  23. $result = exec('file -ib '.escapeshellarg(__FILE__));
  24. if ( 0 === strpos($result,'text/x-PHP') OR 0 === strpos($result,'text/x-c++')) {
  25. return 'linux';
  26. }
  27. $result = exec('file -Ib '.escapeshellarg(__FILE__));
  28. if ( 0 === strpos($result,'text/x-c++')) {
  29. return 'bsd';
  30. }
  31. }
  32. return 'internal';
  33. }
  34. function _getMimeType($path) {
  35. global $mime;
  36. $fmime = _getMimeDetect();
  37. switch($fmime) {
  38. case 'finfo':
  39. $finfo = finfo_open(FILEINFO_MIME);
  40. if ($finfo)
  41. $type = @finfo_file($finfo,$path);
  42. break;
  43. case 'mime_content_type':
  44. $type = mime_content_type($path);
  45. break;
  46. case 'linux':
  47. $type = exec('file -ib '.escapeshellarg($path));
  48. break;
  49. case 'bsd':
  50. $type = exec('file -Ib '.escapeshellarg($path));
  51. break;
  52. default:
  53. $pinfo = pathinfo($path);
  54. $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
  55. $type = isset($mime[$ext]) ? $mime[$ext] : 'unkown';
  56. break;
  57. }
  58. $type = explode(';',$type);
  59. //需要加上这段,因为如果使用mime_content_type函数获取一个不存在的$path时会返回'application/octet-stream'
  60. if ($fmime != 'internal' AND $type[0] == 'application/octet-stream') {
  61. $pinfo = pathinfo($path);
  62. $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
  63. if (!empty($ext) AND !empty($mime[$ext])) {
  64. $type[0] = $mime[$ext];
  65. }
  66. }
  67. return $type[0];
  68. }
  69. $path = '1.txt'; //实际上当前路径并不存在1.txt
  70. var_dump(_getMimeType($path));
  71. /*End of PHP*/
  72. /*** 代码来自编程之家 jb51.cc(jb51.cc) ***/

猜你在找的PHP相关文章