PHP finfo_file中的DOCX文件类型是application/zip

Woo*_*Dzu 16 php validation file-type docx

您好我正在尝试通过finfo_file函数验证上传的文件类型.

但是当发送.docx文件时,文件类型为:

application/zip
Run Code Online (Sandbox Code Playgroud)

代替:

application/vnd.openxmlformats-officedocument.wordprocessingml.document
Run Code Online (Sandbox Code Playgroud)

我该如何改变这种行为?

Kin*_*nch 12

就我而言,供应商特定的文件类型(vnd.)没有标准化(由任何RFC),因此不被file_info()覆盖..docx是一个压缩的xml格式,这就是原因,为什么file_info()返回application_zip(什么是完全正确的).您可以解压缩文件并测试结果的mime类型,但这将导致xml(文档也是如此)以及文档使用的其他文件.不同的XML格式之间的差异file_info()必须分析其内容,它必须知道,它看起来如何,到目前为止.

  • 即使php知道mime-type:`finfo_file()`旨在获取文件的类型,而不是其内容.它也不容易明确地区分这种复杂的结构.文档本身只是`application/xml`,因此您也需要查看和分析它. (3认同)

Jay*_*y K 9

这适用于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
Run Code Online (Sandbox Code Playgroud)

然后,告诉php使用/ etc/magic作为它的数据库:

$finfo = finfo_open(FILEINFO_MIME,"/etc/magic");
Run Code Online (Sandbox Code Playgroud)


Dar*_*ust 5

这是因为DOCX 一个ZIP文件:

Office Open XML文件是ZIP兼容的OPC包,包含XML文档和其他资源.

与Open Office文件一样,文档是以结构化和明确定义的方式包含各种资源的ZIP.因此,当您尝试识别文件内容时,首先会看到它是一个ZIP文件.然后,您需要查看ZIP内部以确定它是DOCX还是OpenOffice文件.

作为替代方案,您可以查看文件扩展名:如果您将文件标识为ZIP并且扩展名恰好是.doc或者.docx您可以将其视为OOXML文件.