Get content of docx file which saved in mysql dabase as blob type in php

主宰稳场 提交于 2019-12-02 01:23:47

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.

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

(source of the code: http://www.botskool.com/geeks/how-extract-text-docx-or-odt-files-using-php)

edi9999

Docx is a zipped file type See Tag Wiki

That's why you can't get the content of the document from the raw content.

Isma'el

I found this solution :

"update blob_table set blob_col='LOAD_FILE('$tmp_name')";

where $tmp_name is the file you upload, and this is the answer for this 6 years old question, using LOAD_FILE function. may be this is a newly added function to mysql.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!