php如何获取文件MIME类型

墨辰丷
发布: 2023-03-31 11:52:02
原创
2500 人浏览过

本篇文章主要介绍php如何获取文件MIME类型,感兴趣的朋友参考下,希望对大家有所帮助。

本文实例讲述了php准确获取文件MIME类型的方法。具体实现方法如下:

<?php
$mime = array (
    //applications
    &#39;ai&#39;  => &#39;application/postscript&#39;,
    &#39;eps&#39;  => &#39;application/postscript&#39;,
    &#39;exe&#39;  => &#39;application/octet-stream&#39;,
    &#39;doc&#39;  => &#39;application/vnd.ms-word&#39;,
    &#39;xls&#39;  => &#39;application/vnd.ms-excel&#39;,
    &#39;ppt&#39;  => &#39;application/vnd.ms-powerpoint&#39;,
    &#39;pps&#39;  => &#39;application/vnd.ms-powerpoint&#39;,
    &#39;pdf&#39;  => &#39;application/pdf&#39;,
    &#39;xml&#39;  => &#39;application/xml&#39;,
    &#39;odt&#39;  => &#39;application/vnd.oasis.opendocument.text&#39;,
    &#39;swf&#39;  => &#39;application/x-shockwave-flash&#39;,
    // archives
    &#39;gz&#39;  => &#39;application/x-gzip&#39;,
    &#39;tgz&#39;  => &#39;application/x-gzip&#39;,
    &#39;bz&#39;  => &#39;application/x-bzip2&#39;,
    &#39;bz2&#39;  => &#39;application/x-bzip2&#39;,
    &#39;tbz&#39;  => &#39;application/x-bzip2&#39;,
    &#39;zip&#39;  => &#39;application/zip&#39;,
    &#39;rar&#39;  => &#39;application/x-rar&#39;,
    &#39;tar&#39;  => &#39;application/x-tar&#39;,
    &#39;7z&#39;  => &#39;application/x-7z-compressed&#39;,
    // texts
    &#39;txt&#39;  => &#39;text/plain&#39;,
    &#39;php&#39;  => &#39;text/x-php&#39;,
    &#39;html&#39; => &#39;text/html&#39;,
    &#39;htm&#39;  => &#39;text/html&#39;,
    &#39;js&#39;  => &#39;text/javascript&#39;,
    &#39;css&#39;  => &#39;text/css&#39;,
    &#39;rtf&#39;  => &#39;text/rtf&#39;,
    &#39;rtfd&#39; => &#39;text/rtfd&#39;,
    &#39;py&#39;  => &#39;text/x-python&#39;,
    &#39;java&#39; => &#39;text/x-java-source&#39;,
    &#39;rb&#39;  => &#39;text/x-ruby&#39;,
    &#39;sh&#39;  => &#39;text/x-shellscript&#39;,
    &#39;pl&#39;  => &#39;text/x-perl&#39;,
    &#39;sql&#39;  => &#39;text/x-sql&#39;,
    // images
    &#39;bmp&#39;  => &#39;image/x-ms-bmp&#39;,
    &#39;jpg&#39;  => &#39;image/jpeg&#39;,
    &#39;jpeg&#39; => &#39;image/jpeg&#39;,
    &#39;gif&#39;  => &#39;image/gif&#39;,
    &#39;png&#39;  => &#39;image/png&#39;,
    &#39;tif&#39;  => &#39;image/tiff&#39;,
    &#39;tiff&#39; => &#39;image/tiff&#39;,
    &#39;tga&#39;  => &#39;image/x-targa&#39;,
    &#39;psd&#39;  => &#39;image/vnd.adobe.photoshop&#39;,
    //audio
    &#39;mp3&#39;  => &#39;audio/mpeg&#39;,
    &#39;mid&#39;  => &#39;audio/midi&#39;,
    &#39;ogg&#39;  => &#39;audio/ogg&#39;,
    &#39;mp4a&#39; => &#39;audio/mp4&#39;,
    &#39;wav&#39;  => &#39;audio/wav&#39;,
    &#39;wma&#39;  => &#39;audio/x-ms-wma&#39;,
    // video
    &#39;avi&#39;  => &#39;video/x-msvideo&#39;,
    &#39;dv&#39;  => &#39;video/x-dv&#39;,
    &#39;mp4&#39;  => &#39;video/mp4&#39;,
    &#39;mpeg&#39; => &#39;video/mpeg&#39;,
    &#39;mpg&#39;  => &#39;video/mpeg&#39;,
    &#39;mov&#39;  => &#39;video/quicktime&#39;,
    &#39;wm&#39;  => &#39;video/x-ms-wmv&#39;,
    &#39;flv&#39;  => &#39;video/x-flv&#39;,
    &#39;mkv&#39;  => &#39;video/x-matroska&#39;
    );
function _getMimeDetect() {
  if (class_exists(&#39;finfo&#39;)) {
    return &#39;finfo&#39;;
  } else if (function_exists(&#39;mime_content_type&#39;)) {
    return &#39;mime_content_type&#39;;
  } else if ( function_exists(&#39;exec&#39;)) {
    $result = exec(&#39;file -ib &#39;.escapeshellarg(__FILE__));
    if ( 0 === strpos($result, &#39;text/x-php&#39;) OR 0 === strpos($result, &#39;text/x-c++&#39;)) {
      return &#39;linux&#39;;
    }
    $result = exec(&#39;file -Ib &#39;.escapeshellarg(__FILE__));
    if ( 0 === strpos($result, &#39;text/x-php&#39;) OR 0 === strpos($result, &#39;text/x-c++&#39;)) {
      return &#39;bsd&#39;;
    }
  }
  return &#39;internal&#39;;
}
function _getMimeType($path) {
  global $mime;
  $fmime = _getMimeDetect();
  switch($fmime) {
    case &#39;finfo&#39;:
      $finfo = finfo_open(FILEINFO_MIME);
      if ($finfo) 
        $type = @finfo_file($finfo, $path);
      break;
    case &#39;mime_content_type&#39;:
      $type = mime_content_type($path);
      break;
    case &#39;linux&#39;:
      $type = exec(&#39;file -ib &#39;.escapeshellarg($path));
      break;
    case &#39;bsd&#39;:
      $type = exec(&#39;file -Ib &#39;.escapeshellarg($path));
      break;
    default:
      $pinfo = pathinfo($path);
      $ext = isset($pinfo[&#39;extension&#39;]) ? strtolower($pinfo[&#39;extension&#39;]) : &#39;&#39;;
      $type = isset($mime[$ext]) ? $mime[$ext] : &#39;unkown&#39;;
      break;
  }
  $type = explode(&#39;;&#39;, $type);
  //需要加上这段,因为如果使用mime_content_type函数来获取一个不存在的$path时会返回&#39;application/octet-stream&#39;
  if ($fmime != &#39;internal&#39; AND $type[0] == &#39;application/octet-stream&#39;) {
    $pinfo = pathinfo($path); 
    $ext = isset($pinfo[&#39;extension&#39;]) ? strtolower($pinfo[&#39;extension&#39;]) : &#39;&#39;;
    if (!empty($ext) AND !empty($mime[$ext])) {
      $type[0] = $mime[$ext];
    }
  }
  return $type[0];
}
$path = &#39;1.txt&#39;; //实际上当前路径并不存在1.txt
var_dump(_getMimeType($path));
/*End of php*/
登录后复制

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php使用历史记录功能

php流程控制及数学运算的方法

PHP垃圾回收机制的相关问题

以上是php如何获取文件MIME类型的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板