Home  >  Article  >  Backend Development  >  Implementation code for obtaining network files using PHP_PHP tutorial

Implementation code for obtaining network files using PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:41:591056browse

Copy the code The code is as follows:

//Set the file we will use
$srcurl = "http://localhost/index.php";
$tempfilename = "tempindex.html";
$targetfilename = "index.html";
?>


<br>Generating <?php echo("$targetfilename"); ?> <br>


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!




www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321048.htmlTechArticleCopy the code as follows: ?php //Set the file we will use $srcurl = "http://localhost /index.php"; $tempfilename = "tempindex.html"; $targetfilename = "index.html"; ? HTML...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn