PHP 文件创建/写入
PHP 写入文件 - fwrite()
fwrite() 函数用于向文件写入。
fwrite() 的第一个参数包含要写入的文件的名称,第二个参数是要写入的字符串。
下面的例子将一些名字写入一个名为 "newfile.txt" 的新文件:
实例
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Bill Gates\n";
fwrite($myfile, $txt);
$txt = "Steve Jobs\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
请注意,我们两次写入文件 "newfile.txt"。每次我们向文件写入时,我们都发送了字符串 $txt,该字符串首先包含 "Bill Gates",然后包含 "Steve Jobs"。完成写入后,我们使用 fclose() 函数关闭了文件。
如果我们打开 "newfile.txt" 文件,它将如下所示:
Bill Gates
Steve Jobs