1// LOCK_EX will prevent anyone else writing to the file at the same time
2// PHP_EOL will add linebreak after each line
3$txt = "data-to-add";
4$myfile = file_put_contents('logs.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
5
6// Second option is this
7$myfile = fopen("logs.txt", "a") or die("Unable to open file!");
8$txt = "user id date";
9fwrite($myfile, "\n". $txt);
10fclose($myfile);
1<?php
2
3$file = 'myFile.txt';
4$text = "This is my Text\n";
5file_put_contents($file, $text, FILE_APPEND | LOCK_EX);
6
7// adds "This is my Text" and a linebreak to the end of "myFile.txt"
8// "LOCK_EX" prevents anyone else writing to the file at the same time
9
10?>
11
1$log_content="This line is logged on 2020-08-14 09:55:00";
2$myfile = fopen("log.txt", "a") or die("Unable to open file!");
3fwrite($myfile, $log_content);
4fclose($myfile);