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

我与影子孤独终老i 提交于 2019-12-02 06:53:02

问题


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.


回答1:


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)




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/18913736/get-content-of-docx-file-which-saved-in-mysql-dabase-as-blob-type-in-php

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