I am having trouble implementing a script that checks if a static page exists and if not, runs $script_file (see code below) to generate the page first, and then sends it to the client, and writes it to a cache folder.
The trouble is that the script code is being sent to the client and is written to a file rather than the script output (html) being sent and written. I think that the problem lies in the "fgets" area of the script. The script is based on Nir Yariv's article (www.zend.com/zend/art/scriptcaching.php). I have tried using "file_get_contents", "fread", and "file" functions instead of "fgets" with no success. The $script_file works fine by itself (it runs an ODBC query and displays the data in an html table) but the problem occurs when it is invoked by the script below.
My dev box is running W2000 / Apache 1.3.27 / PHP 4.3.1
Any help is appreciated. The code shown below is after it has been determined that the static file doesn't exist so the $script_file needs to be invoked to create the page. Here is the code excerpt:
<?php
// access the script
$read = fopen ( "$script_file" , "rb" );
// Declare an empty variable for the script output
$HTML_output = "";
// Read the php script output (the html) while displaying it.
// This is where the problem lies. The php is not being
// interpretted but instead is sent to the client.
while ($line = fgets ($read , 256))
{
$HTML_output .= $line;
echo $line;
}
fclose ( $read );
// Finally, save the HTML output in a file.
$write = fopen ( $static_file_path , "wb" );
// If not, echo Sorry!
if ( !$write )
{
echo ( "Sorry, couldn't open $static_file_path for writing" );
exit;
}
// If so, lock the static file . . .
if ( !flock ( $write , LOCK_EX + LOCK_NB ))
{
echo ( "Could not lock $static_file" );
exit;
}
// . . . and write all the HTML into it.
fwrite ( $write , $HTML_output , strlen ( $HTML_output ) );
// Release lock
flock ( $write , LOCK_UN );
// Close file
fclose ( $write );
}
?>