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

后端 未结 3 1004
南旧
南旧 2021-01-22 15:25

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 u

相关标签:
3条回答
  • 2021-01-22 15:32

    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.

    0 讨论(0)
  • 2021-01-22 15:42

    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.

    0 讨论(0)
  • 2021-01-22 15:52

    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)

    0 讨论(0)
提交回复
热议问题