(view source code of readword_php as plain text)
<?php
$progver = "1.00";
$commandline = ( !isset( $_SERVER ) || !isset( $_SERVER['REMOTE_ADDR'] ) );
function read_word( $wordfile ) {
global $commandline;
$errormessage = "Unable to instantiate Word";
if ( !$commandline ) {
$errormessage = "<p>{$errormessage}</p>\n\n";
}
$word = new com( "Word.Application" ) or die( $errormessage );
$wordversion = "[Microsoft Word, Version {$word->Version}]";
if ( !$commandline ) {
$wordversion = "<p style=\"color: silver;\">{$wordversion}</p>\n\n";
}
print( $wordversion );
try {
$word->Visible = 1;
$document = $word->Documents->open( $wordfile );
if ( $commandline ) {
print( "[{$_POST['filename']}]\n\n" );
} else {
print( "<p style=\"color: silver;\">[{$_POST['filename']}]</p>\n\n" );
}
if ( $word->Documents->count( ) == 1 ) {
foreach ( $document->Sentences as $sentence ) {
if ( $commandline ) {
print( "{$sentence}\n\n" );
} else {
print( "<p>{$sentence}</p>\n\n" );
}
}
}
} catch ( Exception $e ) {
if ( $commandline ) {
print( "{$e->getMessage( )}\n\n" );
} else {
print( "<pre>{$e->getMessage( )}</pre>\n" );
}
} finally {
if ( $word->Documents->count( ) > 0 ) {
$word->Documents->close( );
}
$word->Quit( );
$word = null;
}
}
function show_help( ) {
global $progver;
print( "\nreadword.php, Version {$progver} for Windows\n" );
print( "Read a Word document and display the text in a console or web page\n\n" );
print( "Usage: PHP \"" . $_SERVER['SCRIPT_NAME'] . "\" word_doc\n\n" );
print( " or: [ START ] http://example.com/" . basename( __FILE__ ) . "\n\n" );
print( "Where: word_doc is the full path to an MS Word document\n" );
print( " http://example.com/" . basename( __FILE__ ) . " is the url to this script.\n\n" );
print( "Notes: When run on the command line, this script requires a Word document\n" );
print( " as its only command line argument; when run in a browser, the file\n" );
print( " name has to be entered interactively.\n" );
print( " In command line mode only the first argument is used, any excess\n" );
print( " arguments will be ignored.\n" );
print( " This Windows-only script requires MS Word to read the Word document.\n\n" );
print( "Written by Rob van der Woude\n" );
print( "https://www.robvanderwoude.com\n" );
}
if ( $commandline ) {
array_shift( $argv );
if ( count( $argv ) == 0 || $argv[0] == "/?" ) {
show_help( );
} else {
read_word( $argv[0] );
}
} else {
print( "<html>\n" );
print( "<head>\n" );
print( "<title>Read Word document in web page with PHP by Rob van der Woude</title>\n" );
print( "</head>\n" );
print( "<body>\n\n" );
print( "<form action=\"{$_SERVER['SCRIPT_NAME']}\" method=\"POST\">\n" );
print( "<input name=\"loadfile\" type=\"hidden\" value=\"1\" />\n" );
if ( isset( $_POST ) && isset( $_POST['loadfile'] ) && $_POST['loadfile'] == '1' && isset( $_POST['filename'] ) && trim( $_POST['filename'] ) != '' ) {
read_word( $_POST['filename'] );
} else {
print( '<p title="Enter the full path for the Word document and click \'OK\'">Word file name: ' );
print( '<input type="text" name="filename" />' );
print( ' ' );
print( '<input type="submit" value="OK" /></p>' );
}
print( "</form>\n\n" );
print( "</body>\n" );
print( "</html>" );
}
?>
page last modified: 2024-04-16; loaded in 0.0185 seconds