PHP 中的 fputs() 函数
phpprogrammingserver side programming更新于 2025/5/15 10:52:17
使用 PHP 中的 fpus() 函数写入打开的文件。它是 fwrite() 的别名。成功时,fputs() 函数返回写入的字节数。失败时返回 FALSE。
fputs() 函数在文件末尾或达到指定长度时停止(以先到者为准)。
语法
fputs(file_pointer, string, length)
参数
file_pointer − 使用 fopen() 创建的文件指针。必需。
string − 要写入的字符串。必需。
length − 写入的最大字节数。可选。
返回
fputs() 函数在成功时返回写入的字节数。失败时返回 FALSE。
示例
<?php
$file_pointer = fopen("new.txt","w");
echo fputs($file,"This is demo text!");
fclose($file_pointer);
?>
以下是输出。它返回写入的字节数。
输出
18
让我们看另一个示例,该示例将指定数量的字节写入文件。内容也被读取并显示。
示例
<?php
$file_pointer = fopen("new.txt","w");
echo fputs($file,"This is demo text!",4);
fclose($file_pointer);
fopen("new.txt", "r");
echo fread($file_pointer, filesize("new.txt"));
fclose($file_pointer);
?>
输出
4

