Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php 准确获取文件MIME类型的方法

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

怪我咯
怪我咯asal
2017-07-14 14:32:052933semak imbas

MIME (Multipurpose Internet Mail Extensions) 是描述消息内容类型的因特网标准。

MIME 消息能包含文本、图像、音频、视频以及其他应用程序专用的数据。

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

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

Atas ialah kandungan terperinci php 准确获取文件MIME类型的方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:php 魔术方法使用简介Artikel seterusnya:php 中的类型约束简介