How to View a Word Document From PHP

by Jim Campbell
Display Word document from PHP.

Display Word document from PHP.

Word documents are rich-text documents that allow you to create newsletters, contracts and other customer communication. You can display these Word documents in the user's browser using the PHP programming language. The PHP programming language provides developers with a free resource to create dynamic webpages. You can create dynamic content by displaying a Word document for the web reader.

1

Create your Word application and file name variable. These variables are required to point PHP to the file and use the internal Word functions. The following code creates the variables:

$app= new COM("Word.Application"); $file = "/worddoc.doc";

2

Open the Word document and set its attribute to visible. The following code opens the Word document:

$app->visible = true; $app->Documents->Open($file);

3

Print out the Word document to the web browser. The following code prints the Word document to the browser, so your readers can view the file:

$app->ActiveDocument->PrintOut();

4

Close the document and quit the application. The Word document still displays in the user's browser, but the memory resources are freed on the server. The following code closes your document:

$app->ActiveDocument->Close(); $app->Quit();

About the Author

Jim Campbell has been a computer engineer for over five years. He excels in hardware repair, computer programming and troubleshooting, and software design. He is currently attending Florida Atlantic University, pursuing a master's degree in computer and electrical engineering and fine-tuning his technical writing abilities.

Photo Credits

  • photo_camera computer image by blaine stiger from Fotolia.com