How to use PHP to obtain files on the network
// Set the file to be used
$ Srcurl = "http: // localhost/index. php ";
$ Tempfilename = "tempindex.html ";
$ Targetfilename = "index.html ";
?>
<Br> Generating <? Php echo ("$ targetfilename");?> <Br>
Generating ...
// First delete the temporary files that may be left behind by the last operation.
// This process may prompt errors, so we use @ to prevent errors.
@ Unlink ($ tempfilename );
// Load the dynamic version with a URL request.
// Before we receive the relevant content, the Web server will process PHP
// (Because we are simulating a Web browser in essence ),
// So we will get a static HTML page.
// 'R' indicates that we only need to read this "file.
$ Dynpage = fopen ($ srcurl, 'r ');
// Handle 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 1 MB of data (exceeding this data volume usually means an error ).
$ Htmldata = fread ($ dynpage, 1024*1024 );
// Close the connection to the source "file" after the work is completed.
Fclose ($ dynpage );
// Open the temporary file (created in this process at the same time) for writing (note the usage of 'W ).
$ Tempfile = fopen ($ tempfilename, 'w ');
// Handle errors
If (! $ Tempfile ){
Echo ("
Unable to open temporary file ".
"($ Tempfilename) for writing. Static page ".
"Update aborted!
");
Exit ();
}
// Write data on the static page to the temporary file
Fwrite ($ tempfile, $ htmldata );
// Close the temporary file after writing.
Fclose ($ tempfile );
// If so, we should have successfully written a temporary file,
// Now we can use it to overwrite the original static page.
$ OK = copy ($ tempfilename, $ targetfilename );
// Delete the temporary file.
Unlink ($ tempfilename );
?>
Static page successfully updated!