How to obtain files on the Internet with PHP? _PHP Tutorial

WBOY
Release: 2016-07-21 14:55:20
Original
850 people have browsed it

Can PHP get files on the Internet? How to obtain files on the Internet with PHP? Please see the example code:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com]
//Set the file we will use
$srcurl = "http://localhost/index.php";
$tempfilename = "tempindex.html";
$targetfilename = "index.html";
?>


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


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


//First delete the 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/364444.htmlTechArticleCan PHP obtain files on the Internet? How to obtain files on the Internet with PHP? Please see the example code: Copy to Clipboard Quoted content: [www.veryhuo.com] !-- generateindex.php...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template