在文本文件中创建或写入/附加 [英] Create or write/append in text file
本文介绍了在文本文件中创建或写入/附加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网站,每次用户登录或注销时,我都会将其保存到文本文件中.
I have a website that every time a user logs in or logs out I save it to a text file.
如果代码不存在,我的代码将无法添加数据或创建文本文件.这是示例代码
My code doesn't work in appending data or creating a text file if it does not exist.. Here is the sample code
$myfile = fopen("logs.txt", "wr") or die("Unable to open file!");
$txt = "user id date";
fwrite($myfile, $txt);
fclose($myfile);
在我再次打开它之后,它似乎没有追加到下一行.
It seems it does not append to next line after I open it again.
另外,我认为在2个用户同时登录的情况下也会出现错误,这会影响打开文本文件并随后保存吗?
Also I think it would also have an error in a situation when 2 users login at the same time, would it affect opening the text file and saving it afterwards?
推荐答案
尝试如下操作:
$txt = "user id date";
$myfile = file_put_contents('logs.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
这篇关于在文本文件中创建或写入/附加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文