Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change file contents in PHP?

Tags:

file

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).

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 !

like image 405
Misha Moroshko Avatar asked Dec 14 '25 11:12

Misha Moroshko


1 Answers

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

  • open the file (i.e., fopen('data.txt','w+') )
  • read the data (fread)
  • write the data (fwrite)
  • close the file (fclose)
like image 91
Matt Avatar answered Dec 17 '25 01:12

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!