How to use PHP to obtain files on the network
<! -- Generateindex. php -->
<? Php
// Set the file to be used
$ 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 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 ("<P> Unable to load $ srcurl. Static page ".
"Update aborted! </P> ");
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 ("<P> Unable to open temporary file ".
"($ Tempfilename) for writing. Static page ".
"Update aborted! </P> ");
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 );
?>
<P> Static page successfully updated! </P>
</BODY>
</HTML>
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