PHP에서 파일의 MIME 유형을 정확하게 얻는 방법

怪我咯
풀어 주다: 2023-03-13 22:30:01
원래의
2890명이 탐색했습니다.

MIME(Multi Purpose 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*/
로그인 후 복사

위 내용은 PHP에서 파일의 MIME 유형을 정확하게 얻는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!