Saturday, February 23, 2013

Reading a Word file in PHP (Windows COM)

If you want to read a .doc and .docx file for your PHP application we need to convert it into plain text for reading. To convert .doc and .docx file into plain text , we can make use of a COM object

The code goes something like this:

$word = new COM("word.application") or die("Unable to instantiate Word"); //Opening word
$word->Documents->Open($filename); //Open the file using word application
$new_filename = substr($filename,0,-4).".txt"; //New txt file with .txt extension
$word->Documents[1]->SaveAs($new_filename,2); //saving the word as a text file
$word->Documents[1]->Close(false); //close the file
$word->Quit(); //quit word
$word = NULL; //destroy the variable content
$fh = fopen($new_filename, 'r'); //Open the file
$filedata = fread($fh, filesize($new_filename)); //store the plain text content in a variable
fclose($fh); //close the txt file
unlink($new_filename); //Unlink the file

Save this as index.php and open it in your localhost (Probably WAMP Server)

I'm 99% sure it wont work :P (Since you have not configured your DCOM in your windows machine)

To configure DCOM object:

1. Open command prompt(cmd)(Windows +R)

2. Type dcomcnfg (Distributed Component Object Model Configuration) and press Enter

3.In the left panel go to Component Services -> Computers -> My Computer ->DCOM Config

4.Search for Microsoft word 97-2003 Document and Right click Properties

5.Then go to Identity tab and select The interactive user and then Click Apply

6.Now your are done and good to go. I'm sure 99.99% it will work. If you find any problems please comment or contact me for clarifications.

Stay tuned.


  1. Nice blog...Very useful information is providing by ur blog. Great beginning php tutorials Very clear and helpful for beginners.

  2. "Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.php jobs in hyderabad.