Function to get the content of a docx in php

2019-06-14 14:47发布

问题:

 private function read_docx($filename) {
    var_dump($filename);
    $striped_content = '';
    $content = '';

    $zip = zip_open($filename);

    if (!$zip || is_numeric($zip))
        return false;

    while ($zip_entry = zip_read($zip)) {

        if (zip_entry_open($zip, $zip_entry) == FALSE)
            continue;

        if (zip_entry_name($zip_entry) != "word/document.xml")
            continue;

        $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

        zip_entry_close($zip_entry);
    }// end while

    zip_close($zip);

    $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
    $content = str_replace('</w:r></w:p>', "\r\n", $content);
    $striped_content = strip_tags($content);

    return $striped_content;
}

I have used the above code for displaying .docx content in my view using the below code:

  <div style="padding: 10px">
    <?php
    $fileName = $details->resume_name;
    $path = FCPATH . "uploads/document/";
    // i have also tried.. $path = base_url() . "uploads/document/"; and $path = FCPATH . "uploads/document/";
    $fullPath = $path . $fileName;                     
    echo $CI->docx->read_docx($fullPath);                      
    ?>
   </div>

But when I run this code it shows me nothing and the result is empty....

Please help me to solve this problem.. or suggest me any similar way to read the content of .docx file and display it in a simple html page or in a view

回答1:

its working now.. i am answering my own question so that any one else can get help from it...

the problem is with the path here $path = FCPATH . "uploads/document/";

which I have replaced with $path = "./uploads/document/";



标签: php zip docx