Jerahmeel Acebuche Jerahmeel Acebuche - 4 months ago 20
PHP Question

php create or write/append in text file

I have a website that every time a user login or logout I will save it to a text file.

my code doesn't work in appending the data or creates a text file if 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);

but it seems it does not append to next line after i open it again.

also. I think it would also have an error in a situation like what if 2 users login on the same time would it affect opening the text file and saving it after?


Try something like this :

 $txt = "user id date";
 $myfile = file_put_contents('logs.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);