我有一个网站,每次用户登录或注销时,我都会将其保存到文本文件中。如果不存在附加数据或创建文本文件,我的代码将不起作用。这是示例代码$myfile = fopen("logs.txt", "wr") or die("Unable to open file!");$txt = "user id date";fwrite($myfile, $txt);fclose($myfile);再次打开它后,它似乎没有追加到下一行。另外,我认为在两个用户同时登录的情况下也会出现错误,会影响打开文本文件并随后保存吗?
3 回答
![?](http://img1.sycdn.imooc.com/545845b40001de9902200220-100-100.jpg)
慕莱坞森
TA贡献1810条经验 获得超4个赞
尝试这样的事情:
$txt = "user id date";
$myfile = file_put_contents('logs.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
![?](http://img1.sycdn.imooc.com/545863080001255902200220-100-100.jpg)
holdtom
TA贡献1805条经验 获得超10个赞
使用a模式。它代表append。
$myfile = fopen("logs.txt", "a") or die("Unable to open file!");
$txt = "user id date";
fwrite($myfile, "\n". $txt);
fclose($myfile);
- 3 回答
- 0 关注
- 305 浏览
添加回答
举报
0/150
提交
取消