2013-04-27 2 views
4

Je dois déterminer quel est le type de fichier téléchargéDéterminer le fichier excel type MIME

Lorsque fichier télécharger .xlsx, ce code:

echo $_FILES['uploaded_file']['type']."<br>"; 
echo mime_content_type($_FILES['uploaded_file']['tmp_name']); 

retours:

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 
application/vnd.ms-excel 

Comme je sais (de ici PHP xls, xlsx, ppt, pptx headers), application/vnd.ms-excel n'est pas .xlsx, mais .xls fichier type mime. Donc, pourquoi retourne mime_content_type() la fonction application/vnd.ms-excel pour le fichier .xlsx? où est la vérité?

Répondre

3

mime_content_type() n'est pas particulièrement précis, et a été déprécié au profit de Fileinfo()'s mime_content_type; bien que personnellement, j'ouvre le fichier et tester explicitement pour certains éléments de données dans les fichiers qui pourraient ne pas être inclus dans le cadre de la signature mime_magic détails

+0

Merci beaucoup, '@Rikesh, tucuxi' merci aussi – RIKI

1

Comme vous pouvez le voir l'avertissement sur la page de la fonction mime_content_type est obsolète maintenant & il a été remplacé par finfo fonction.

$finfo = new finfo(); 
$fileinfo = $finfo->file($file, FILEINFO_MIME); 

Pour installer l'extension finfo.

pecl install fileinfo 
6

Utilisation FileInfo au lieu de mime_content_type (qui est deprecated).

En ce qui concerne mime-types et extensions,

application/vnd.ms-excel           xls xlb xlt 
application/vnd.ms-excel.addin.macroEnabled.12     xlam 
application/vnd.ms-excel.sheet.binary.macroEnabled.12    xlsb 
application/vnd.ms-excel.sheet.macroEnabled.12     xlsm 
application/vnd.ms-excel.template.macroEnabled.12     xltm 
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx 

(disponible à /etc/mime.types dans votre serveur web linux)

2

Voici une enveloppe qui correctement identifier les documents Microsoft Office 2007. Il est trivial et facile à utiliser, modifier et ajouter plus d'extensions de fichiers/types de fichiers.

function get_mimetype($filepath) { 
    if(!preg_match('/\.[^\/\\\\]+$/',$filepath)) { 
     return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $filepath); 
    } 
    switch(strtolower(preg_replace('/^.*\./','',$filepath))) { 
     // START MS Office 2007 Docs 
     case 'docx': 
      return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; 
     case 'docm': 
      return 'application/vnd.ms-word.document.macroEnabled.12'; 
     case 'dotx': 
      return 'application/vnd.openxmlformats-officedocument.wordprocessingml.template'; 
     case 'dotm': 
      return 'application/vnd.ms-word.template.macroEnabled.12'; 
     case 'xlsx': 
      return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; 
     case 'xlsm': 
      return 'application/vnd.ms-excel.sheet.macroEnabled.12'; 
     case 'xltx': 
      return 'application/vnd.openxmlformats-officedocument.spreadsheetml.template'; 
     case 'xltm': 
      return 'application/vnd.ms-excel.template.macroEnabled.12'; 
     case 'xlsb': 
      return 'application/vnd.ms-excel.sheet.binary.macroEnabled.12'; 
     case 'xlam': 
      return 'application/vnd.ms-excel.addin.macroEnabled.12'; 
     case 'pptx': 
      return 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; 
     case 'pptm': 
      return 'application/vnd.ms-powerpoint.presentation.macroEnabled.12'; 
     case 'ppsx': 
      return 'application/vnd.openxmlformats-officedocument.presentationml.slideshow'; 
     case 'ppsm': 
      return 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12'; 
     case 'potx': 
      return 'application/vnd.openxmlformats-officedocument.presentationml.template'; 
     case 'potm': 
      return 'application/vnd.ms-powerpoint.template.macroEnabled.12'; 
     case 'ppam': 
      return 'application/vnd.ms-powerpoint.addin.macroEnabled.12'; 
     case 'sldx': 
      return 'application/vnd.openxmlformats-officedocument.presentationml.slide'; 
     case 'sldm': 
      return 'application/vnd.ms-powerpoint.slide.macroEnabled.12'; 
     case 'one': 
      return 'application/msonenote'; 
     case 'onetoc2': 
      return 'application/msonenote'; 
     case 'onetmp': 
      return 'application/msonenote'; 
     case 'onepkg': 
      return 'application/msonenote'; 
     case 'thmx': 
      return 'application/vnd.ms-officetheme'; 
      //END MS Office 2007 Docs 

    } 
    return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $filepath); 
}