PHP/File Directory/fputs
Содержание
A Simple Text-File Hit Counter
<?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<BR>";
} else {
echo "An Error has occurred.<BR>";
}
?>
Storing data on a remote server
<?php
$file = fopen("ftp://ftp.php.net/incoming/outputfile", "w");
if (!$file) {
echo "<p>Unable to open remote file for writing.\n";
exit;
}
fputs($file, "$HTTP_USER_AGENT\n");
fclose($file);
?>
Using the fputs() Function
<?php
function custom_echo($string) {
$output = "Custom Message: $string";
fputs(STDOUT, $string);
}
custom_echo("This is my custom echo function!");
?>
Writing and Appending to a File
<html>
<head>
<title>Writing and Appending to a File</title>
</head>
<body>
<div>
<?php
$filename = "data.txt";
print "Writing to $filename<br/>";
$fp = fopen ( $filename, "w" ) or die ( "Couldn"t open $filename" );
fwrite ( $fp, "Hello world\n" );
fclose ( $fp );
print "Appending to $filename<br/>";
$fp = fopen ( $filename, "a" ) or die ( "Couldn"t open $filename" );
fputs ( $fp, "And another thing\n" );
fclose ( $fp );
?>
</div>
</body>
</html>