Using PHP to get the network file implementation code

  • 2020-03-31 20:18:12
  • OfStack


<?php 
//Set the file we will 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 any temporary files that may have been left over from the previous operation.
//This process may prompt errors, so we use @ to prevent errors.
@unlink($tempfilename); 
//Load the dynamic version with a request for a URL.
//The Web server processes PHP before we receive the relevant content
//(because we're essentially emulating a Web browser),
//So what we're going to get is a static HTML page.
//'r' indicates that we only want to read 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 the URL into a PHP variable.
//Specify that we will read 1MB of data (more than this is usually an error).
$htmldata = fread($dynpage, 1024*1024); 
//When we are done, close the connection to the source file.
fclose($dynpage); 
//Open the temporary file (also created in the process) for writing (note the use 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 the data for the 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 successfully written a temporary file,
//Now we can use it to override the original static page.
$ok = copy($tempfilename, $targetfilename); 
//Finally, delete the temporary file.
unlink($tempfilename); 
?> 
<P>Static page successfully updated!</P> 
</BODY> 
</HTML> 

Related articles: