I have a script that creates a HTML for every row in a DB query result. I had this working normally but for some reason I am now having strange problems.

Firstly, there are 2 files: a generator.php file that runs a loop which in turn calls another PHP file and saves the output of that as HTML file with a special naming format.

the second file is the resultscache.php file. This file could be called on its own for any set of results but I need to create HTML from it as a form of caching system.

Below is the generator file (apologies for the length)

PHP Code:

$months = array(
"name"=>"December",     "num"=>"12",     "num_of_days"=>"31")
$years = array(2002);

$years as $year)
$months as $month)
$i=30$i<=$month['num_of_days']; $i++)
$startdate date ("dmY"mktime (0,0,0,$month['num'],$i,$year));
$startyear date ("Y"mktime (0,0,0,$month['num'],$i,$year));
$startmonth date ("m"mktime (0,0,0,$month['num'],$i,$year));
$startday date ("d"mktime (0,0,0,$month['num'],$i,$year));

$srcurl "http://localhost:9000/horseracingresults/rebuild/resultscache.php";
$query "SELECT DISTINCT Track, Stdate FROM mkt_result WHERE Stdate = '$startdate'";
$result ifx_query($query$conn_id);

//give $track the same value as whatever corresponds with $row[track] at this iteration of the while loop
while($row ifx_fetch_row($result)){
$track $row[track];

//check to see if date directory already exists before we try to create it
if (!file_exists("html/".$startyear)) {
//make date directory
mkdir ("html/".$startyear0777); 

if (!
file_exists("html/".$startyear."/".$startmonth)) {
//make date directory
mkdir ("html/".$startyear."/".$startmonth0777); 

if (!
file_exists("html/".$startyear."/".$startmonth."/".$startday)) {
//make date directory
mkdir ("html/".$startyear."/".$startmonth."/".$startday0777); 

//fix track for HTML name
$track ereg_replace(" ""_"trim($track));

$tempfilename "tempindex.html";
$targetfilename "html/".$startyear."/".$startmonth."/".$startday."/".$track.$startdate.".html";

//put space back in track for DB query
$track ereg_replace("_"" "$track);
<title>Generating HTML...</title>
<p>Generating <?= $targetfilename?>...</p>

//change the txt file into a PHP file that replaces placeholder text 
with the correct PHP generated Track name (generated from the query)
$tempfile "resultscache.php";
$fp1 = @fopen($tempfile'w');
$fp = @fopen("http://localhost:9000/horseracingresults/rebuild/resultscache.txt""w"
or die(
"Couldn't open file"); 
$data fread($fpfilesize($fp)); 
$data .= fread($fp100000); 
$toreplace "-track-";
$replacewith $track;
$data ereg_replace($toreplace$replacewith$data);
$toreplace1 "-date-";
$replacewith1 $startdate;
$data ereg_replace($toreplace1$replacewith1$data);
$ok fwrite($fp1$data);
//generate php file end


$dynpage fopen($srcurl'r');

"<p>Unable to load $srcurl. Static page "."update aborted!</p>");

$htmldata fread($dynpage1024*1024);


$tempfile fopen($tempfilename'w');

    echo (
"<p>Unable to open temporary file");


$ok copy($tempfilename$targetfilename);


//FTP the file
// set up basic connection
$conn_id ftp_connect(""); 

$track ereg_replace(" ","_",$track);

// login with username and password
$login_result ftp_login($conn_idpicturespaddy); 
"C:/Program Files/nusphere/apache/nsdocs/horseracingresults/rebuild/html/"

//make date directory


$upload ftp_put
//end db result loop

//end dates array foreach
Basically, this file first sets some date variables Then it runs through the array using foreach. next it decides what directories to create or if they exist already, ignore them.

Next it opens a resultscache.txt file. It replaces the words -track- and -date- with the date variables set at the top of the script. It then saves this with the replaced -track- etc. as resultscache.php and runs this PHP file. It then saves the output of the PHP file as a HTML file in a certain name format ie: TRACKNAME12012002.html

The contents of the resultscache.php file are fine, but the problem seems to lie in the code above.

It gives an error in the first HTML file generated and it creates every subsequent file from the foreach loop without any problems. The first HTML file is generated with the correct name but when I open that file it has a parse error. There is no parse errors in that code as i checked the file on its own and also, the other HTML files are generated from the same page and they are fine.

I am totally stuck with this so if anyone can help you will be a lifesaver!