PHP/File Directory/fputs
Содержание
A Simple Text-File Hit Counter
<source lang="html4strict">
<?php
function retrieveCount($hitfile) { $fr = @fopen($hitfile, "r"); if(!$fr) { $count = 0; } else { $count = fgets($fr, 4096); fclose($fr); } $fr = @fopen($hitfile, "w"); if(!$fr) return false; $count++; if(@fputs($fr, $count) == -1) return false; fclose($fr); return $count; } $count = retrieveCount("hitcount.dat"); if($count !== false) { echo "This page has been visited $count times
"; } else { echo "An Error has occurred.
"; }
?>
</source>
Storing data on a remote server
<source lang="html4strict">
<?php $file = fopen("ftp://ftp.php.net/incoming/outputfile", "w");
if (!$file) {echo "
Unable to open remote file for writing.\n"; exit; } fputs($file, "$HTTP_USER_AGENT\n"); fclose($file); ?> </source>
Using the fputs() Function
<source lang="html4strict">
<?php
function custom_echo($string) { $output = "Custom Message: $string"; fputs(STDOUT, $string); } custom_echo("This is my custom echo function!");
?>
</source>
Writing and Appending to a File
<source lang="html4strict">
<html> <head> <title>Writing and Appending to a File</title> </head> <body>
<?php
$filename = "data.txt";
print "Writing to $filename
";
$fp = fopen ( $filename, "w" ) or die ( "Couldn"t open $filename" );
fwrite ( $fp, "Hello world\n" );
fclose ( $fp );
print "Appending to $filename
";
$fp = fopen ( $filename, "a" ) or die ( "Couldn"t open $filename" );
fputs ( $fp, "And another thing\n" );
fclose ( $fp );
?>
</body> </html>
</source>