扫码关注官方订阅号
PHP读取DOC文件时出问题。 百度的安装第三方包 来实现 系统提示 ** is not a word document.... 求助
走同样的路,发现不同的人生
可以使用一下PHPWord,要确保你的php版本是 PHP 5.3+
if($ext == 'docx'){ //docx文件可以直接读取 $contents = $this->extracttext($file); }elseif($ext == 'doc'){ //doc文件,需要安装antiword软件来读取 $contents = shell_exec( "antiword -m UTF-8 $file" ); }else{ $contents = file_get_contents($file); }
function extracttext($filename) { $ext = end(explode('.', $filename)); if($ext == 'docx') $dataFile = "word/document.xml"; else $dataFile = "content.xml"; $zip = new ZipArchive; if (true === $zip->open($filename)) { if (($index = $zip->locateName($dataFile)) !== false) { $text = $zip->getFromIndex($index); $xml = DOMDocument::loadXML($text, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING); return strip_tags($xml->saveXML()); } $zip->close(); } return false; }
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
可以使用一下PHPWord,要确保你的php版本是 PHP 5.3+
if($ext == 'docx'){
//docx文件可以直接读取
$contents = $this->extracttext($file);
}elseif($ext == 'doc'){
//doc文件,需要安装antiword软件来读取
$contents = shell_exec( "antiword -m UTF-8 $file" );
}else{
$contents = file_get_contents($file);
}