How to change file contents in PHP?

I save some information about users in the file (for example, the number of times the user went through the login page, time of the last visit, etc.).

I want to read this information from a file and update it (add 1 to the counter and change the last time I visited).

My question is: can I do this without opening the file twice?

I open it for the first time to read the content, and then open it again to overwrite the contents with the updated ones.

Thank!

+3
source share
1 answer

Yes, you do this only by opening the file once as follows:

  • open the file (i.e. fopen ('data.txt', 'w +'))
  • read data (fread)
  • write data (fwrite)
  • close file (fclose)
+2
source

Source: https://habr.com/ru/post/1749880/


All Articles