Home > Backend Development > PHP Tutorial > How to get file MIME type in php

How to get file MIME type in php

墨辰丷
Release: 2023-03-31 11:52:02
Original
2511 people have browsed it

This article mainly introduces how to obtain the file MIME type in php. Interested friends can refer to it. I hope it will be helpful to everyone.

The example in this article describes how PHP can accurately obtain the MIME type of a file. The specific implementation method is as follows:

<?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*/
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

Related recommendations:

php uses the history recording function

php process control and mathematical operations Method

Related issues with PHP garbage collection mechanism

The above is the detailed content of How to get file MIME type in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template