Comment obtenir le type de fichier MIME en php

墨辰丷
Libérer: 2023-03-31 11:52:02
original
2450 Les gens l'ont consulté

Cet article explique principalement comment obtenir le type de fichier MIME en php. Les amis intéressés peuvent s'y référer. J'espère qu'il sera utile à tout le monde.

L'exemple de cet article décrit comment obtenir avec précision le type MIME d'un fichier en PHP. La méthode de mise en œuvre spécifique est la suivante :

<?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*/
Copier après la connexion

Résumé : Ce qui précède est l'intégralité du contenu de cet article, j'espère qu'il sera utile à l'apprentissage de chacun.

Recommandations associées :

fonction d'historique d'utilisation de PHP

méthode de contrôle de processus PHP et d'opérations mathématiques

Problèmes liés au mécanisme de récupération de place PHP

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!