PHP 函数fwrite()
fwrite()函数可以向打开的文件写入内容。该函数可以在文件末尾或者达到指定长度时停止写入,以先到者为准。此函数可以返回写入的字节数,或在失败时返回false。
语法
int fwrite ( resource handle , stringstring [, int $length ] )
此函数可以将字符串的内容写入指向句柄的文件流中。
示例1
<?php
file = fopen("/PhpProject/sample.txt", "w");
echo fwrite(file, "Hello Tutorialspoint!!!!!");
fclose($file);
?>
输出
25
示例2
<?php
filename = "/PhpProject/sample.txt";somecontent = "Add this to the file\n";
if(is_writable(filename)) {
if(!handle = fopen(filename, 'a')) {
echo "Cannot open file (filename)";
exit;
}
if(fwrite(handle,somecontent) === FALSE) {
echo "Cannot write to file (filename)";
exit;
}
echo "Success, wrote (somecontent) to file (filename)";
fclose(handle);
} else {
echo "The file $filename is not writable";
}
?>
输出
Success, wrote (Add this to the file) to file (/PhpProject/sample.txt)