Copy Code code as follows:
<?php
Set up the files we are going to use
$srcurl = "http://localhost/index.php";
$tempfilename = "tempindex.html";
$targetfilename = "index.html";
?>
<HTML>
<HEAD>
<TITLE>
Generating <?php Echo ("$targetfilename");?>
</TITLE>
</HEAD>
<BODY>
<p>generating <?php Echo ("$targetfilename");?>...</p>
<?php
First delete the temporary files that might be left over from the last operation.
This process may prompt an error, so we use @ To prevent errors.
@unlink ($tempfilename);
Mount the dynamic version with a request from a URL.
The Web server handles PHP before we receive the relevant content
(because essentially we're emulating a Web browser),
So what we're going to get is a static HTML page.
' R ' indicates that we only require a read operation on this "file".
$dynpage = fopen ($srcurl, ' R ');
Handling Errors
if (! $dynpage) {
Echo ("<p>unable to load $srcurl. Static page.
"Update aborted!</p>");
Exit ();
}
Read the contents of this URL into a PHP variable.
Specifies that we will read 1MB of data (more than this amount of data generally means that there is a mistake).
$htmldata = Fread ($dynpage, 1024*1024);
When we have finished working, turn off the connection to source "file".
Fclose ($dynpage);
Open the temporary file (established in the process) to write (note the usage of ' w ').
$tempfile = fopen ($tempfilename, ' w ');
Handling Errors
if (! $tempfile) {
Echo ("<p>unable to open temporary file").
"($tempfilename) for writing. Static page.
"Update aborted!</p>");
Exit ();
}
Writes data from a static page to a temporary file
Fwrite ($tempfile, $htmldata);
When the write is complete, close the temporary file.
Fclose ($tempfile);
If we get here, we should have succeeded in writing 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);
?>
<p>static Page successfully updated!</p>
</BODY>
</HTML>