//Set the file we will use
$srcurl = "http://localhost/index.php";
$tempfilename = "tempindex.html";
$targetfilename = "index.html";
?>
Generating
Generating ...
//Delete first Temporary files that may be left over from the last operation.
//This process may prompt errors, so we use @ to prevent errors.
@unlink($tempfilename);
//Load the dynamic version through a URL request.
//Before we receive the relevant content, the web server will process PHP
//(because essentially we are simulating a web browser),
//so what we will get is A static HTML page.
//'r' indicates that we only require read operations on this "file".
$dynpage = fopen($srcurl, 'r');
//Handling errors
if (!$dynpage) {
echo("
Unable to load $srcurl. Static page ".
"update aborted!
");
exit();
}
//Read the content of this URL into a PHP variable.
//Specify that we will read 1MB of data (exceeding this amount of data generally means an error has occurred).
$htmldata = fread($dynpage, 1024*1024);
//When we have finished our work, close the connection to the source "file".
fclose($dynpage);
//Open a temporary file (also created during this process) for writing (note the usage of 'w').
$tempfile = fopen($tempfilename, 'w');
//Handling errors
if (!$tempfile) {
echo("
Unable to open temporary file ".
"($tempfilename) for writing. Static page ".
"update aborted!
");
exit();
}
//Write the data of the static page into a temporary file
fwrite ($tempfile, $htmldata);
//After completing writing, close the temporary file.
fclose($tempfile);
//If we get here, we should have successfully written a temporary file,
//Now we can use it to overwrite the original static page.
$ok = copy($tempfilename, $targetfilename);
//Finally delete this temporary file.
unlink($tempfilename);
?>
Static page successfully updated!