简体   繁体   English

如何在PHP中更改文件内容?

[英]How to change file contents in PHP?

I save in a file some info about users (like number of times user passed the login page, last visited time, and so on). 我在文件中保存了一些关于用户的信息(例如用户通过登录页面的次数,上次访问时间等)。

I want to read this info from the file, and update it (add 1 to the counter, and change the last visited time). 我想从文件中读取此信息并更新它(向计数器添加1,并更改上次访问时间)。

My question is: can I do it without opening the file twice ? 我的问题是:如果不打开文件两次,我可以这样做吗?

I open the first time to read the contents, and then open it again to overwrite the contents with the updated ones. 我第一次打开读取内容,然后再次打开它以覆盖更新的内容。

Thanks ! 谢谢 !

Yes, you do this only opening the file once as follows: 是的,你这样做只打开文件一次,如下所示:

  • open the file (ie, fopen('data.txt','w+') ) 打开文件(即fopen('data.txt','w +'))
  • read the data (fread) 读数据(fread)
  • write the data (fwrite) 写数据(fwrite)
  • close the file (fclose) 关闭文件(fclose)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM