xxxxxxxxxx
$fptr = fopen('myfile2.txt','a');
fwrite($fptr,"hii.. this is appending inside file\n")
fclose($fptr);
xxxxxxxxxx
// LOCK_EX will prevent anyone else writing to the file at the same time
// PHP_EOL will add linebreak after each line
$txt = "data-to-add";
$myfile = file_put_contents('logs.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
// Second option is this
$myfile = fopen("logs.txt", "a") or die("Unable to open file!");
$txt = "user id date";
fwrite($myfile, "\n". $txt);
fclose($myfile);
xxxxxxxxxx
<?php
$file = 'myFile.txt';
$text = "This is my Text\n";
file_put_contents($file, $text, FILE_APPEND | LOCK_EX);
// adds "This is my Text" and a linebreak to the end of "myFile.txt"
// "LOCK_EX" prevents anyone else writing to the file at the same time
?>
xxxxxxxxxx
$log_content="This line is logged on 2020-08-14 09:55:00";
$myfile = fopen("log.txt", "a") or die("Unable to open file!");
fwrite($myfile, $log_content);
fclose($myfile);
Appending line of text to the file using php
xxxxxxxxxx
$file = fopen('/Users/[USERNAME]/Downloads/test.txt', "a");
if ($file) {
fwrite($file, 'Line of text');
fclose($file);
}
fopen, fwrite, fclose php functions to add text to a file, write to file, append to file