gpt4 book ai didi

php - 获取在php中以blob类型保存在mysql数据库中的docx文件的内容

转载 作者:行者123 更新时间:2023-11-29 16:15:46 27 4
gpt4 key购买 nike

我正在 mysql 数据库中将 docx 文件保存为 BLOB 类型。保存后,我试图通过获取提交的内容来查看文件的内容,但它显示了一些不可读的内容。这对于具有扩展名 .doc 的文件来说效果很好,但我不知道为什么它不起作用.docx 文件。如果有任何答案,请帮助提供正确的解释。

最佳答案

进行查询以选择数据,然后将结果放入变量中。使用 file_put_content 获取 docx 文件。只是要小心标题。

阅读它的过程与文档不同。您必须“解压缩”docx 并读取其中的 xml 文件。您可以使用此功能:

<?php

/*Name of the document file*/
$document = 'filename.docx';

/**Function to extract text*/
function extracttext($filename) {
//Check for extension
$ext = end(explode('.', $filename));

//if its docx file
if($ext == 'docx')
$dataFile = "word/document.xml";
//else it must be odt file
else
$dataFile = "content.xml";

//Create a new ZIP archive object
$zip = new ZipArchive;

// Open the archive file
if (true === $zip->open($filename)) {
// If successful, search for the data file in the archive
if (($index = $zip->locateName($dataFile)) !== false) {
// Index found! Now read it to a string
$text = $zip->getFromIndex($index);
// Load XML from a string
// Ignore errors and warnings
$xml = DOMDocument::loadXML($text, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
// Remove XML formatting tags and return the text
return strip_tags($xml->saveXML());
}
//Close the archive file
$zip->close();
}

// In case of failure return a message
return "File not found";
}

echo extracttext($document);
?>

(代码来源:http://www.botskool.com/geeks/how-extract-text-docx-or-odt-files-using-php)

关于php - 获取在php中以blob类型保存在mysql数据库中的docx文件的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54804991/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com