开发者社区> 问答> 正文

php如何识别office 2007格式的文档,报错

php如何识别office 2007格式的文档,比如.docx .xlsx .pptx,我在网上找到,使用二进制来识别文件签名,但是office 2007的文档二进制前4位都是相同的,如何才能够区分开,各自属于哪类文档? 我上网搜到如下代码,想通过如下代码将文档变成zip格,然后在找到 [Content_Types].xml文件,然后通过正则找到ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+> 标签,就可以识别文件的类别了!

/** * Read Docx File * * @param string $file filepath * @return string file content */ function parseWord($file) { $content = ""; $zip = new ZipArchive ( ); if ($zip->open ( $file ) === tr ) { for($i = 0; $i < $zip->numFiles; $i ++) { $entry = $zip->getNameIndex ( $i ); if (pathinfo ( $entry, PATHINFO_BASENAME ) == "[Content_Types].xml") { $zip->extractTo ( pathinfo ( $file, PATHINFO_DIRNAME ) . "/" . pathinfo ( $file, PATHINFO_FILENAME ), array ( $entry ) ); $filepath = pathinfo ( $file, PATHINFO_DIRNAME ) . "/" . pathinfo ( $file, PATHINFO_FILENAME ) . "/" . $entry; $content = strip_tags ( file_get_contents ( $filepath ) ); break; } } $zip->close (); rrmdir ( pathinfo ( $file, PATHINFO_DIRNAME ) . "/" . pathinfo ( $file, PATHINFO_FILENAME ) ); return $content; } else { return ""; } } ?>
<span style=""font-size:14px;"">

展开
收起
一枚小鲜肉帅哥 2020-06-20 20:23:32 1045 0
1 条回答
写回答
取消 提交回答
  • 菜不可及的程序猿

    excel的话,可以用phpexcel。

    至于word我没有研究过,phpexcel中有针对03版和07版excel格式探测的函数,你可以对比下,提取出对你有用的信息。

    2020-06-20 20:24:19
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载
复杂PHP系统性能瓶颈排查及优化 立即下载