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

标签 php mysql blob docx

我正在 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/

相关文章:

php - 如何在向浏览器提供页面之前导出 cytoscape 网络图

php - 为什么 json_encode 返回 null?

php - 奇怪的 Codeigniter 查询转义

php: is_null 和数据库结果

azure - Azure Blob 存储中文件夹的存储统计信息

java - 传递到 Spring JDBC LobHandler 后关闭 Java InputStream?

php - 使用用户名或电子邮件登录

php - 在php中将二进制字符串转换为整数

php - Twig 不渲染 HTML 标签

存储在 Microsoft SQL 中的 Python blob PDF - 转换回 PDF