Obtenir le type mime de nom de fichier en php

J'ai la fonction suivante pour produire le type mime à partir d'un nom de fichier:

    function get_mime_type($file) {
      if (function_exists('finfo_open')) {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mimetype = finfo_file($finfo, $file);
        finfo_close($finfo);
      }
      else {
        $mimetype = mime_content_type($file);
      }
      if (empty($mimetype)) $mimetype = 'application/octet-stream';
      return $mimetype;
    }

J'ai appeler cette fonction à cette partie de mon code:

        $out['uploads'][] = array(
          'filename' => $fldrow['field_value'],
          'mimetype' => get_mime_type($fldrow['field_value']),
          'id'       => $fldrow['ID'],
        );

$fldrow['field_value'] contient 'card.pdf'

Je suis dans l'attente d' 'application/pdf'

Je suis 'application/octet-stream'

J'ai aussi essayé cette approche plus élaborée à l'aide de Mode=1:
PHP type Mime vérification d'autre façon de le faire?

Mêmes résultats en Mode=1 et vide en Mode=0.

Ce que je fait de mal ici?

MODIFIER
Ma solution basée sur Dymen1 de réponse et après avoir regardé d'autres postes dans cette direction est la suivante:

function get_mime_type($filename) {
$idx = explode( '.', $filename );
$count_explode = count($idx);
$idx = strtolower($idx[$count_explode-1]);
$mimet = array( 
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
//images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
//archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
//audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
//adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
//ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'docx' => 'application/msword',
'xlsx' => 'application/vnd.ms-excel',
'pptx' => 'application/vnd.ms-powerpoint',
//open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
if (isset( $mimet[$idx] )) {
return $mimet[$idx];
} else {
return 'application/octet-stream';
}
}
  • Votre Fonction de travail pour moi parfaitement "get_mime_type' 🙂
  • quid de la police? Alors qu'il fonctionne parfaitement pour toutes ces extensions, mais avec woff, j'ai quelques erreurs.
InformationsquelleAutor Craig Tucker | 2016-02-09