DOCX Тип файла в PHP finfo_file – application / zip

привет. Я пытаюсь проверить тип загруженного файла функцией finfo_file .

Но когда отправляется файл .docx, тип файла:

application/zip 

вместо:

 application/vnd.openxmlformats-officedocument.wordprocessingml.document 

как я могу изменить это поведение?

Насколько мне известно, типы файлов конкретных поставщиков (vnd.) Не стандартизированы (каким-либо RFC) и поэтому не рассматриваются файлом file_info (). .docx – это zipped-xml-формат, и это причина, почему file_info() возвращает application_zip (что совершенно правильно). Вы можете разархивировать файл и протестировать mime-тип результата, но это приведет к xml (что совершенно правильно) и другим файлам, которые используются документом. Чтобы различаться между различными форматами XML, file_info() должен был анализировать свой контент, и он должен знать, как он выглядит, что идет далеко.

Это работает на debian. Добавьте это в / etc / magic:

 #------------------------------------------------------------------------------ # $File: msooxml,v 1.1 2011/01/25 18:36:19 christos Exp $ # msooxml: file(1) magic for Microsoft Office XML # From: Ralf Brown <ralf.brown@gmail.com> # .docx, .pptx, and .xlsx are XML plus other files inside a ZIP # archive. The first member file is normally "[Content_Types].xml". # Since MSOOXML doesn't have anything like the uncompressed "mimetype" # file of ePub or OpenDocument, we'll have to scan for a filename # which can distinguish between the three types # start by checking for ZIP local file header signature 0 string PK\003\004 # make sure the first file is correct >0x1E string [Content_Types].xml # skip to the second local file header # since some documents include a 520-byte extra field following the file # header, we need to scan for the next header >>(18.l+49) search/2000 PK\003\004 # now skip to the *third* local file header; again, we need to scan due to a # 520-byte extra field following the file header >>>&26 search/1000 PK\003\004 # and check the subdirectory name to determine which type of OOXML # file we have >>>>&26 string word/ Microsoft Word 2007+ !:mime application/msword >>>>&26 string ppt/ Microsoft PowerPoint 2007+ !:mime application/vnd.ms-powerpoint >>>>&26 string xl/ Microsoft Excel 2007+ !:mime application/vnd.ms-excel >>>>&26 default x Microsoft OOXML !:strength +10 

Затем скажите php использовать / etc / magic как базу данных:

 $finfo = finfo_open(FILEINFO_MIME,"/etc/magic"); 

Это связано с тем, что DOCX является ZIP-файлом :

Файл Office Open XML представляет собой ZIP-совместимый OPC-пакет, содержащий XML-документы и другие ресурсы.

Как и файлы Open Office, документы представляют собой ZIP-файлы, содержащие различные ресурсы, в структурированном и четко определенном виде. Поэтому, когда вы пытаетесь определить содержимое файла, вы сначала видите, что это ZIP-файл. Затем вам нужно будет заглянуть внутрь ZIP, чтобы решить, является ли это файлом DOCX или OpenOffice.

В качестве альтернативы вы можете посмотреть расширение файла: если вы определяете файл как ZIP, а расширение – .doc или .docx то вы можете считать его файлом OOXML.

На apache в .htaccess добавьте это, чтобы исправить проблемы docx и всех других типов файлов:

 AddType application/vnd.ms-word.document.macroEnabled.12 .docm AddType application/vnd.openxmlformats-officedocument.wordprocessingml.document docx AddType application/vnd.openxmlformats-officedocument.wordprocessingml.template dotx AddType application/vnd.ms-powerpoint.template.macroEnabled.12 potm AddType application/vnd.openxmlformats-officedocument.presentationml.template potx AddType application/vnd.ms-powerpoint.addin.macroEnabled.12 ppam AddType application/vnd.ms-powerpoint.slideshow.macroEnabled.12 ppsm AddType application/vnd.openxmlformats-officedocument.presentationml.slideshow ppsx AddType application/vnd.ms-powerpoint.presentation.macroEnabled.12 pptm AddType application/vnd.openxmlformats-officedocument.presentationml.presentation pptx AddType application/vnd.ms-excel.addin.macroEnabled.12 xlam AddType application/vnd.ms-excel.sheet.binary.macroEnabled.12 xlsb AddType application/vnd.ms-excel.sheet.macroEnabled.12 xlsm AddType application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx AddType application/vnd.ms-excel.template.macroEnabled.12 xltm AddType application/vnd.openxmlformats-officedocument.spreadsheetml.template xltx