Home arrow PHP arrow Page 4 - PHP and COM

Export Potential - PHP

You've already seen how PHP can be used to interface with Java components and JavaBeans. But here's something you didn't know - PHP can (shock shock! horror horror!) even be used to interface with Microsoft COM objects on the Windows platform. Will this be a happy marriage? Read on to find out.

TABLE OF CONTENTS:
  1. PHP and COM
  2. Striving To Excel
  3. The Number Game
  4. Export Potential
  5. Keeping It Simple
  6. Access-ing The Web
  7. All For One, And One For All
  8. New Coins For Old
  9. Link Zone
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 15
April 23, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Let's look at something a little more interesting. I've always considered Microsoft Word to be one of the best editing tools to ever come out of Redmond - and one of its nicest features, especially for Web newbies, is the ability to quickly convert a formatted Word document into an HTML file suitable for use on a Web site.

That's where my next example comes in. It uses Word's built-in export-to-HTML feature in combination with a PHP script to give new meaning to the term "tighter Web integration". Watch, and be awed!

The first step here is to write a script that displays a list of all the Word documents available on the system. There's nothing fancy here - this is just very basic usage of PHP's file and directory functions.



<?php // where are the files stored? $PathToDocumentFolder = "C:\\My Stuff\\Documents"; ?> <html> <head> <basefont face="Arial"> <title>My Documents</title> </head> <body> <h2>My Documents</h2> <table border="2" cellpadding="5" cellspacing="1" width="90%"> <tr> <th width="55%" align="left">Name</th> <th width="20%" align="center">&nbsp;</th> <th width="20%" align="center">&nbsp;</th> </tr> <?php // get a handle to the document folder $dHandle = dir($PathToDocumentFolder); // iterate through the file list while($dContent=$dHandle->read()) { // only list the .doc files if(substr($dContent,-4)== ".doc") { ?> <tr> <td width="55%" align="left"><?php echo $dContent; ?></td> <td width="20%" align="center"><a href="<?php echo $PathToDocumentFolder."\\".$dContent; ?>">View as Word</a></td> <td width="20%" align="center"><a href="<?php echo "htmlviewer.php?DocumentPath=".$PathToDocumentFolder."\\".$dContent; ?>">View as HTML</a></td> </tr> <?php } } // close the handle to the directory $dHandle->close(); ?> </table> </body> </html>
Here's what it looks like:



The first step here is to initialize a variable containing the location of the directory containing the Word documents.

<?php // where are the files stored? $PathToDocumentFolder = "C:\\My Stuff\\Documents\\"; ?>
Next, a table is generated and a PHP "while" loop is used to populate it with a file list of Word documents in that directory.

<?php // get a handler to the document folder $dHandle = dir($PathToDocumentFolder); // iterate through the file list while($dContent=$dHandle->read()) { // only list the .doc files if(substr($dContent,-4)== ".doc") { ?> <tr> <td width="55%" align="left"><?php echo $dContent; ?></td> <td width="20%" align="center"><a href="<?php echo $PathToDocumentFolder."\\".$dContent; ?>">View as Word</a></td> <td width="20%" align="center"><a href="<?php echo "htmlviewer.php?DocumentPath=".$PathToDocumentFolder."\\".$dContent; ?>">View as HTML</a></td> </tr> <?php } } ?>
Within this code block, the first step is to create a directory handle for the named location. This "pseudo-object" gives me access to several methods that will allows us to read the contents of the directory, move back to the root folder, close this handle and so on.

Once I have the object created, I've used a "while" loop to read the contents of the directory, and an "if" test to filter out all the non-Word files, on the basis of the ".doc" file extension. Each of the files in the final list is then displayed as a separate row in the table, complete with links to view it, either as a Word document within the browser itself, or as an HTML document via the "htmlviewer.php" script.

This "htmlviewer.php" script is where all the meat really is. Let's take a look at that next.

 
 
>>> More PHP Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: