©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
(PHP 4, PHP 5, PHP 7)
fputs — fwrite() 的别名
此函数是该函数的别名: fwrite() 。
[#1] Nile River [2010-12-11 10:54:01]
This will create a visitor count for your webpage
<?php
$filename = 'sitevisitors.txt';
if (file_exists($filename))
{
$count = file('sitevisitors.txt');
$count[0] ++;
$fp = fopen("sitevisitors.txt", "w");
fputs ($fp, "$count[0]");
fclose ($fp);
echo $count[0];
}
else
{
$fh = fopen("sitevisitors.txt", "w");
if($fh==false)
die("unable to create file");
fputs ($fh, 1);
fclose ($fh);
$count = file('sitevisitors.txt');
echo $count[0];
}
?>
[#2] rembert at floating-point dot nl [2002-11-21 04:03:12]
Adding to Adam (Nedstat):
fputs without the length parm just writes all data up to but not including the first 0 byte it encounters.
Therefore fputs($fp,"hello\0world") will only write hello to $fp whereas
fputs($fp,"hello\0world",11) will write both words.
If this doesn't make sense for you, remember strings are always terminated with that 0 byte. Binary data doesn't have such a terminator byte as a 0 byte can be a completely valid piece of data therefore you always need to know the length of the binary datapart.
BTW strlen() is binary safe, so you can use that to get the length of the binary data part as well - this is different from C which counts the number of chars up to the 0 byte. So the example above could also be written as:
fputs($fp,"hello\0world",strlen("hello\0world"))
[#3] yvan dot rivard at cesart dot com [2001-04-17 14:52:32]
This might seem silly for experienced users, but this bugged me for about two hours (searching and trying to debug the damn thing).
If you're on a Windows system doing tests on a Linux/Apache setup, and you're writing stuff to a text file (then load that file to see if your content is being written properly) and you realize only your first string is in there, your problem is not your Windows, not Linux and not Apache. It's probably Samba (the thing that makes it possible for your Linux and Windows boxes to talk to each other) that's caching your file... Try viewing your file via telnet or simply copy your file then read that copy.
You can see content is being written to your file anyway, because even though you don't see your new content (using notepad for example), you do see your filesize increasing as you add more text to your file.
[#4] [2000-10-08 00:07:56]
You can use here-documents in PHP but not for printing to a file pointer.
There's a lot to say on them so just go to the page: http://www.php.net/manual/language.types.string.php
[#5] Doug at Here dot Com [2000-06-30 17:46:36]
The way I understand fputs (which is purported as an alias to fwrite which can be "Binary Safe")...
<?php
$fp = fopen("something.txt","w");
$string = "Hello World\\n"; // escape the slash from being magically
// being transformed into a newline
fwrite($fp, $string); // will proccess /n as newline ...
fwrite($fp, $string, strlen($string)); // will write the entire string to file without changing the '/n'
// into a single byte for newline on Unix-like machines or CR/LF on Win32 machines
?>
Hope this helps explain the definition of "could be Binary Safe". This is the reason why you must specify the length!
--Doug
[#6] bschuetz at affinity dot net [1998-12-22 19:30:56]
Length is required if you are writing out a lot of data. For instance, if you are base64 decoding an email attachment and writing it out to a file you have to specify the length if the file is over a certian size or else the binary data will be corrupt.
fputs($fp,$binary,strlen($binary));
Will yield the desired results.
When tested with a 1k the length wasn't required but with a larger file, around 27k it needs the length to work properly.