Read doc and docx files in Cake php

This post is intended to provide a reliable way to read the contents of .docx and .doc files. The content can be imported as html and inserted in ckeditor or any other such tool for further processing enabling users to edit the contents and save them.

The contents are read from the doc and docx file retaining their formatting.

How to read doc and docx file in php

  1. Downlaod the Zend library and put it in the vendors folder.
  2. We will be using the Zend Live Docx service for this process for which we need to create an account on Its free of cost.
  3. Next we need to create a file named zend_include_path.php file and put it in vendors folder.
  4. In our zend_include_path.php we will have to write following snippet

ini_set('include_path',ini_get('include_path') . PATH_SEPARATOR . dirname(__FILE__));


5. Next, in our controller we need to write

class ReadFileController extends AppController { 
  * Function to read doc files using Zend Live Docx service and return the contents as html 
  * @author Ankit Tiwari 
  * @email [email protected] 
  * @param string $docPath The path of the file to read 
  * @return string the contents of the doc file in HTML format 
  public function getImportedFileContent($docPath)
     App::import('Vendor', 'zend_include_path'); 
     App::import('Vendor', 'Zend_Service_LiveDocx_MailMerge', true, false, 'Zend/Service/LiveDocx/MailMerge.php' ); 
     App::import('Vendor', 'Zend_Service_LiveDocx_Exception', true, false, 'Zend/Service/LiveDocx/Exception.php' ); 
     // call the class mailmerge 
     $mailMerge = new Zend_Service_LiveDocx_MailMerge(); 
     // sets the login username and password here to access services 
     // sets the docx file. 
     $document = $mailMerge->retrieveDocument('html');
     return $document;