当前位置:首页 > PHP教程 > php应用 > 列表

php准确获取文件MIME类型的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-28 20:35:06 浏览: 评论:0 

这篇文章主要介绍了php准确获取文件MIME类型的方法,涉及php针对文件属性操作的相关技巧,需要的朋友可以参考下。

本文实例讲述了php准确获取文件MIME类型的方法,分享给大家供大家参考,具体实现方法如下:

  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*/

Tags: php获取文件MIME

分享到:

相关文章