Using PHP to get network file implementation code _php tips

Source: Internet
Author: User
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.