How does PHP implement getting files on the network?

Source: Internet
Author: User

<span>can PHP get the files on the network? How does PHP implement getting files on the network? Take a look at the example code:


<!--generateindex.php-->


<?php


//Set the file 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);


//The dynamic version is loaded via a URL request.


//Before we receive the relevant content, the Web server will process PHP


//(because essentially we are impersonating 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 error


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 the source "file".


fclose ($dynpage);


//Open temporary file (established in the process) to write (note ' W ' usage).


$tempfile = fopen ($tempfilename, ' W ');


//Handling error


if (! $tempfile) {


Echo ("<p>unable to open temporary file".


"($tempfilename) for writing. Static page.


"Update aborted!</p>");


exit ();





//writes static page data to the temporary file


fwrite ($tempfile, $htmldata);

after
//finish writing, 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.