How to use PHP to obtain files on the network

Source: Internet
Author: User
Tags html page

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 ";
Generating <? Php echo ("$ targetfilename");?>
<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>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.