获取以blob类型存储在mysql dabase中的docx文件的内容,在php中 [英] Get content of docx file which saved in mysql dabase as blob type in php

查看:331
本文介绍了获取以blob类型存储在mysql dabase中的docx文件的内容,在php中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am saving docx file as BLOB type in mysql dadabase. after the saveing i am trying to see the content of the file through fetching the content of filed but it is showing some unreadable content.This this is working well for file having extention .doc but i don't know why it is not working for the .docx file.If any answer please help with proper explanation.

推荐答案

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

Make a query to select the data, then put the result in a variable. Use file_put_content to get the docx file. Just be carefull with header.

要阅读该文档,该过程与文档不同.您必须解压缩" docx并读取其中的xml文件.您可以使用此功能:

To read it, the process is different from a doc. You have to "unzip" the docx and read the xml file inside it. You can use this function:

<?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 )

这篇关于获取以blob类型存储在mysql dabase中的docx文件的内容,在php中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆