2011-09-01 175 views
0

我無法通過PHP檢測xlsx Excell文件的mimetype,因爲它是zip存檔。通過PHP檢測excell .xlsx文件mimetype

文件utilite

file file.xlsx 
file.xlsx: Zip archive data, at least v2.0 to extract 

PECL的FileInfo

$finfo = finfo_open(FILEINFO_MIME_TYPE); 
finfo_file($finfo, "file.xlsx"); 
application/zip 

如何驗證呢?打開包裝並查看結構?但是如果它是arcbomb?

+0

你必須解壓文件。 PHP不會偷看你的文件。如果它是一個arcbomb,你只需要處理它。 –

+0

這裏是一個半重複的:http://stackoverflow.com/questions/6595183/docx-file-type-in​​-php-finfo-file-is-application-zip –

+0

好的,謝謝。我希望ms使用額外的信息。並且nautilus中的文件信息表示它是「Microsoft Excel工作表(application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)」,但它使用擴展名列表。 – Shara

回答

1

我知道這適用於zip文件,但我不太確定xlsx文件。這是值得一試:

要列出一個zip壓縮包中的文件:

$zip = new ZipArchive; 
$res = $zip->open('test.zip'); 
if ($res === TRUE) { 
    for ($i=0; $i<$zip->numFiles; $i++) { 
     print_r($zip->statIndex($i)); 
    } 
    $zip->close(); 
} else { 
    echo 'failed, code:' . $res; 
} 

這將打印的所有文件是這樣的:

Array 
(
    [name] => file.png 
    [index] => 2 
    [crc] => -485783131 
    [size] => 1486337 
    [mtime] => 1311209860 
    [comp_size] => 1484832 
    [comp_method] => 8 
) 

正如你可以在這裏看到,它給人的sizecomp_size爲每個檔案。如果是歸檔炸彈,這兩個數字之間的比例將是天文數字。您可以簡單地設置最大解壓縮文件大小的任意數量的限制,如果超出該數量,則跳過該文件並將錯誤消息發回給用戶,否則繼續解壓縮。有關更多信息,請參見the manual

+0

謝謝,是不是打開時將arch打開到temp中? – Shara

+0

不,它只是讀取文件。提取,你會使用ZipArchive :: extractTo(http://www.php.net/manual/en/function.ziparchive-extractto.php) – Mike

+0

謝謝,邁克。我怎樣才能讓你的答案被接受? – Shara

-2

這是一個可以正確識別Microsoft Office 2007文檔的包裝器。使用,編輯和添加更多文件擴展/ mimetypes是簡單而直接的。

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