(PHP 4, PHP 5, PHP 7)
fwrite — 写锟斤拷锟侥硷拷锟斤拷锟缴帮拷全锟斤拷锟节讹拷锟斤拷锟斤拷锟侥硷拷锟斤拷
$handle
, string $string
[, int $length
] ) : int
fwrite() 锟斤拷 string
锟斤拷锟斤拷锟斤拷写锟斤拷
锟侥硷拷指锟斤拷 handle
锟斤拷锟斤拷
handle
string
The string that is to be written.
length
锟斤拷锟街革拷锟斤拷锟�
length
锟斤拷锟斤拷写锟斤拷锟斤拷
length
锟斤拷锟街节伙拷锟斤拷写锟斤拷锟斤拷 string
锟皆猴拷写锟斤拷突锟酵V癸拷锟斤拷雍锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
注锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
length
锟斤拷锟斤拷锟斤拷锟斤拷 magic_quotes_runtime
锟斤拷锟斤拷选锟筋将锟斤拷锟斤拷锟皆o拷锟斤拷
string
锟叫碉拷斜锟竭斤拷锟斤拷锟结被锟斤拷去锟斤拷
fwrite() 锟斤拷锟斤拷写锟斤拷锟斤拷址锟斤拷锟斤拷锟斤拷锟斤拷执锟斤拷锟绞憋拷蚍祷锟� FALSE
锟斤拷
Note:
Writing to a network stream may end before the whole string is written. Return value of fwrite() may be checked:
<?php
function fwrite_stream($fp, $string) {
for ($written = 0; $written < strlen($string); $written += $fwrite) {
$fwrite = fwrite($fp, substr($string, $written));
if ($fwrite === false) {
return $written;
}
}
return $written;
}
?>
Note:
锟斤拷锟斤拷锟街讹拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟侥憋拷锟侥硷拷锟斤拷系统锟较o拷锟斤拷 Windows锟斤拷 锟斤拷锟侥硷拷时锟斤拷fopen() 锟斤拷锟斤拷锟斤拷 mode 锟斤拷锟斤拷要锟斤拷锟斤拷 'b'锟斤拷
Note:
If
handle
was fopen()ed in append mode, fwrite()s are atomic (unless the size ofstring
exceeds the filesystem's block size, on some platforms, and as long as the file is on a local filesystem). That is, there is no need to flock() a resource before calling fwrite(); all of the data will be written without interruption.
Note:
If writing twice to the file pointer, then the data will be appended to the end of the file content:
<?php
$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
// the content of 'data.txt' is now 123 and not 23!
?>
Example #1 一锟斤拷锟津单碉拷 fwrite() 锟斤拷锟斤拷
<?php
$filename = 'test.txt';
$somecontent = "锟斤拷锟斤拷锟叫╋拷锟斤拷值锟斤拷募锟絓n";
// 锟斤拷锟斤拷锟斤拷锟斤拷要确锟斤拷锟侥硷拷锟斤拷锟节诧拷锟揭匡拷写锟斤拷
if (is_writable($filename)) {
// 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷铮拷锟斤拷墙锟绞癸拷锟斤拷锟斤拷模式锟斤拷$filename锟斤拷
// 锟斤拷耍锟斤拷募锟街革拷虢拷锟斤拷锟斤拷募锟斤拷锟侥┪诧拷锟�
// 锟角撅拷锟角碉拷锟斤拷锟斤拷使锟斤拷fwrite()锟斤拷时锟斤拷$somecontent锟斤拷要写锟斤拷牡胤锟斤拷锟�
if (!$handle = fopen($filename, 'a')) {
echo "锟斤拷锟杰达拷锟侥硷拷 $filename";
exit;
}
// 锟斤拷$somecontent写锟诫到锟斤拷锟角打开碉拷锟侥硷拷锟叫★拷
if (fwrite($handle, $somecontent) === FALSE) {
echo "锟斤拷锟斤拷写锟诫到锟侥硷拷 $filename";
exit;
}
echo "锟缴癸拷锟截斤拷 $somecontent 写锟诫到锟侥硷拷$filename";
fclose($handle);
} else {
echo "锟侥硷拷 $filename 锟斤拷锟斤拷写";
}
?>