php写入文件的方法
php 写入文件 - fwrite()
fwrite() 函数用于写入文件。
fwrite() 的第一个参数包含要写入的文件的文件名,第二个参数是被写的字符串。
下面的例子把姓名写入名为 newfile.txt 的新文件中:
实例
<?php$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 gatessteve jobs
更多php知识,请访问php教程!
以上就是php写入文件的方法的详细内容。