How to Read a DOC File Using PHP

PHP programmers are often seeking ways to extend the functionality of this language outside its comfort zone. PHP is mostly used on Linux operating systems, but the visitors of PHP websites are usually working on Windows or Macintosh systems. A website designed to upload a Microsoft Word file from these users may need to extract the contents of this file and email or process its text using PHP algorithms. But Microsoft Word files, which end in the DOC extension, are not native to Linux or PHP. Bridging these two computer environments is possible with some preparation.

Things You'll Need

  • PHP compiler
  • Antiword MS Word document reader
Show More

Instructions

    • 1

      Verify with your host provider that PHP access is available for your website. Most servers support PHP, but occasionally this service requires an account upgrade. You cannot read a DOC file using PHP without access to the compiler.

    • 2

      Download the Antiword MS Word document reader utility (see References). This open-source program contains programming materials that extend PHP for DOC conversion.

    • 3

      Unzip the Antiword archive to extract its contents.

    • 4

      Upload the entire Antiword file library to the web server. Place the files in the "bin" directory of the host account. This directory is a common repository for utilities and other binary operations that web server users must frequently exploit.

    • 5

      Call the Antiword program in any PHP script designed to read a DOC Microsoft Word document. The command is implemented using a single function. Type "$content = shell_exec('/usr/local/bin/antiword '.$filename);" where "$filename" equals the full file name of the DOC document. The contents of the DOC file will be read into the variable "$content". These variable names, beginning with the "$" symbol, are customizable.

    • 6

      Process the "$content" variable as desired to manipulate the contents of the DOC file. Once the DOC is read by PHP, the full text of the file is available for any form of further activity. The contents can be emailed or stored to a database, for example.

Tips & Warnings

  • Server configurations vary between hosting providers. It is possible that the "bin" directory for your account resides in a different location than the example code included here. The path of your "bin" directory may be something other than "'/usr/local/bin". If so, adjust the "shell_exec" command to suit the particular environment of your server account.

Related Searches:

References

Comments

You May Also Like

  • How to Read MS Office Files in PHP

    Microsoft Office documents are common formats used on the Web and on desktop machines. The PHP languages provides you with dynamic tools...

  • How to Upload DOC Files in PHP

    The PHP, Hypertext Preprocessor, scripting language was created to be used in conjunction with MySQL to make dynamic Web applications. An Apache...

  • How to Read a Text File From a Client Using PHP

    There are two methods you can use for handling file uploads from a client to a server using PHP. The POST method,...

  • How to Read an XLS File Using PHP

    Microsoft Excel is the normal program associate with the filetype XLS, and PHP is a much more widely used general purpose scripting...

  • How to Read a File in PHP

    Reading the contents of a file is an important part of dynamic Web scripting as well as command-line scripting. PHP allows you...

  • How to Read a Text File in PHP

    Reading the contents of a text file is an important part programming, whether you are putting data on a web page, or...

  • How to View Doc Files

    The DOC file is a common file to see on a home computer. It is a text document written in the Microsoft...

Related Ads

Featured