PHP/File Directory/file get contents — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 13:37, 26 мая 2010
Содержание
- 1 Checking for an error from file_get_contents()
- 2 Hello, {name}
- 2.1 Check net speed by loading a file
- 2.2 Fetching a URL with file_get_contents()
- 2.3 file_get_contents.php
- 2.4 file_put_contents( ) writes to a file with the equivalent of fopen( ), fwrite( ), and fclose( ) all in one function, just like file_get_contents( ).
- 2.5 Reading a file into a string
- 2.6 Retrieving a protected page
- 2.7 Retrieving a remote page with file_get_contents()
- 2.8 Use file_get_contents with a URL
Checking for an error from file_get_contents()
<source lang="html4strict">
$page = file_get_contents("page-template.html"); if ($page === false) {
print "Couldn"t load template: $php_errormsg";
} else {
// ... process template here
} //page-template.html <html> <head><title>{page_title}</title></head> <body bgcolor="{color}">
Hello, {name}
</body> </html>
</source>
Check net speed by loading a file
<source lang="html4strict">
<?php
$data = file_get_contents("data.txt"); $fsize = filesize("textfile.txt") / 1024; $start = time(); $stop = time(); $duration = $stop - $start; $speed = round($fsize / $duration,2); echo "Your network speed: $speed KB/sec.";
?>
</source>
Fetching a URL with file_get_contents()
<source lang="html4strict">
<?php $page = file_get_contents("http://www.example.ru/robots.txt"); ?>
</source>
file_get_contents.php
<source lang="html4strict">
<?php
$userfile= file_get_contents("data.txt"); $users = explode("\n",$userfile); foreach ($users as $user) { list($name, $email) = explode(" ", $user); echo "<a href=\"mailto:$email\">$name/a>
"; }
?>
</source>
file_put_contents( ) writes to a file with the equivalent of fopen( ), fwrite( ), and fclose( ) all in one function, just like file_get_contents( ).
<source lang="html4strict">
<?
$filename = "data.txt"; $myarray[ ] = "This is line one"; $myarray[ ] = "This is line two"; $myarray[ ] = "This is line three"; $mystring = implode("\n", $myarray); $numbytes = file_put_contents($filename, $mystring); print "$numbytes bytes written\n";
?>
</source>
Reading a file into a string
<source lang="html4strict">
<?php $people = file_get_contents("people.txt"); if (preg_match("/Names:.*(David|Susannah)/i",$people)) {
print "people.txt matches.";
} ?>
</source>
Retrieving a protected page
<source lang="html4strict">
<?php $url = "http://tom:password@www.example.ru/secrets.php"; $page = file_get_contents($url); ?>
</source>
Retrieving a remote page with file_get_contents()
<source lang="html4strict">
<? $zip = 98052; $weather_page = file_get_contents("http://www.demo.ru/z.php?inputstring=" . $zip); $page = strstr($weather_page,"Detailed Forecast"); $table_start = strpos($page, "<table"); $table_end = strpos($page, "</table>") + 8; print substr($page, $table_start, $table_end - $table_start); ?>
</source>
Use file_get_contents with a URL
<source lang="html4strict">
<? $url = "http://www.demo.ru/"; $page = file_get_contents($url); if (preg_match_all("@<a href="[^"]+">.+?</a>@", $page, $matches)) {
foreach ($matches[0] as $link) { print "$link
\n"; }
} ?>
</source>