2013-04-27 128 views
4

我需要確定什麼是上傳的文件確定excel文件的MIME類型

當上傳.xlsx文件,該代碼的類型:

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

回報:

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

據我所知(從這裏PHP xls, xlsx, ppt, pptx headers),application/vnd.ms-excel不是.xlsx,但.xls文件的MIME類型。

那麼,爲什麼要返回mime_content_type()函數application/vnd.ms-excel.xlsx文件?真相在哪裏?

回答

3

mime_content_type()沒有特別準確,並且有利於Fileinfo()'s mime_content_type已棄用;雖然就個人而言,我打開文件和測試明確了在可能不包括爲mime_magic簽名的一部分的文件中的某些數據元素的細節

+0

非常感謝'@Rikesh,tucuxi'也謝謝你 – RIKI 2013-04-27 12:33:43

1

正如你可以在mime_content_type功能頁面中看到警告是過時現在&它被finfo函數取代。

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

要安裝finfo擴展名。

pecl install fileinfo 
6

使用FileInfo代替mime_content_type(這是deprecated)。

關於MIME類型和擴展,

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 

(可在你的Linux Web服務器/etc/mime.types)

2

下面是一個包裝,將正確識別的Microsoft Office 2007個文檔。這是微不足道的,並直接使用,編輯,並添加更多的文件擴展/ MIME類型。

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); 
}