PHP讀取DOC檔案時出問題。
百度的安裝第三方包 來實現
系統提示 ** is not a word document....
求助
回複內容:
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;}