0

Goal

I have never touched PHP.

My goal is to retrieve BLOB .docx content from MySQL. I have found this resource to help me: Get content of docx file which saved in mysql dabase as blob type in php

I have just installed something called xampp along with Apache and PHP.

Created a folder within htdocs called Techincal. Inside there I have 2 files called test3.php and test.docx

At this moment I am not using MySQL at all. I am trying to see what PHP can do for me.

I have copied the code from the link above.

Code

<?php

/*Name of the document file*/
$document = 'test.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);
?>

When I run it on localhost - been following instructions on other websites on how to run PHP files.

Output

Notice: Only variables should be passed by reference in C:\xampp\htdocs\technical\test3.php on line 9
Testing
1
  • A more reliable way to get a filename extension is to use pathinfo i.e. $ext = pathinfo($filename, PATHINFO_EXTENSION); Commented Dec 19, 2019 at 11:58

2 Answers 2

2

You need to store the result of explode() statement on line no 9 into a variable and then that variable should be passed to end function. This will resolve your problem. Refer the below-corrected code.

       <?php

    /*Name of the document file*/
    $document = 'test.docx';

    /**Function to extract text*/
    function extracttext($filename) {
        //Check for extension
$tmp = explode('.', $filename);
        $ext = end( $tmp );

        //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);
    ?>
Sign up to request clarification or add additional context in comments.

4 Comments

Parse error: syntax error, unexpected '$ext' (T_VARIABLE) in C:\xampp\htdocs\technical\php.php on line 10
There's a semicolon missing after $tmp = explode('.', $filename)
Added the missing semicolon.
Images are stored in word/media/ you need to write code to show them. Use something like - github.com/xylude/Docx-to-HTML
0

Function end() need an array by reference (read: https://www.php.net/manual/en/function.end.php) and you just put there a result of other function. You have to store result of explode() to some variable and that variable put to end().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.