Specifications

LISTING 30.3 Continued
if( !$name || !$score )
{
echo “<h1>Error:</h1>This page was called incorrectly”;
}
else
{
//generate the headers to help a browser choose the correct application
header( “Content-type: application/msword” );
header( “Content-Disposition: inline, filename=cert.rtf”);
$date = date( “F d, Y” );
// open our template file
$filename = “PHPCertification.rtf”;
$fp = fopen ( $filename, “r” );
//read our template into a variable
$output = fread( $fp, filesize( $filename ) );
fclose ( $fp );
// replace the place holders in the template with our data
$output = str_replace( “<<NAME>>”, strtoupper( $name ), $output );
$output = str_replace( “<<Name>>”, $name, $output );
$output = str_replace( “<<score>>”, $score, $output );
$output = str_replace( “<<mm/dd/yyyy>>”, $date, $output );
// send the generated document to the browser
echo $output;
}
?>
This script performs some basic error checking to make sure that all the user details have been
passed in, and then moves to the business of creating the certificate.
The output of this script will be an RTF file rather than an HTML file, so we need to alert the
users browser to this fact. This is important so that the browser can attempt to open the file
with the correct application, or give a Save As type dialog box if it doesnt recognize the
RTF extension.
We specify the MIME type of the file we are outputting using PHPs header() function to
send the appropriate HTTP header as follows:
Building Practical PHP and MySQL Projects
P
ART V
760
36 7842 CH30 3/6/01 3:40 PM Page 760