2011-09-01 5 views
0

zip 아카이브이므로 PHP를 통해 xlsx Excell 파일에 대한 mimetype을 감지 할 수 없습니다.PHP를 통해 excell .xlsx 파일 mimetype 감지

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

에서는 FileInfo

파일 utilite

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

PECL 어떻게 유효성을 검사? 구조의 포장 풀기 및보기? 하지만 아크 폭탄이라면?

+0

파일의 압축을 풀어야합니다. PHP는 파일 안을 들여다 보지 않습니다. 그것이 arcbomb이라면, 당신은 그것을 다루어야 만 할 것입니다. 여기 –

+0

은 반 복제입니다. http://stackoverflow.com/questions/6595183/docx-file-type-in-php-finfo-file-is-application-zip –

+0

알겠습니다. 감사합니다. 나는 ms가 추가 정보를 사용하기를 희망했다. 그리고 노틸러스의 파일 정보는 "Microsoft Excel 워크 시트 (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)"라고했지만 확장 목록을 사용합니다 .. – Shara

답변

1

이 파일은 zip 파일에서 작동하지만 xlsx 파일에 대해서는 확실하지 않습니다.

은 우편 아카이브에있는 파일을 나열하려면 :

$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를 임시로 풀지 않습니까? – Shara

+0

아니요. 그냥 파일을 읽습니다. 압축을 풀려면 ZipArchive :: extractTo (http://www.php.net/manual/en/function.ziparchive-extractto.php)를 사용하십시오. – Mike

+0

감사합니다, 마이크. 당신의 대답을 어떻게 받아 들일 수 있습니까? – Shara

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