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.
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"> </th>
<th width="20%" align="center"> </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.