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$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
2fwrite($myfile, "Content to write to file");
3fclose($myfile);
1$myFile = "testFile.txt";
2$fh = fopen($myFile, 'w') or die("can't open file");
3$stringData = "Bobby Bopper\n";
4fwrite($fh, $stringData);
5$stringData = "Tracy Tanner\n";
6fwrite($fh, $stringData);
7fclose($fh);
8
1// if not file is there then automatic create and write inside it.
2$fptr = fopen('myfile.txt','w');
3fwrite($fptr,"i am writing file in this\n");
4fwrite($fptr,'writing another line.');
5fclose($fptr);
1$data[] = $_POST['data'];
2
3$inp = file_get_contents('results.json');
4$tempArray = json_decode($inp);
5array_push($tempArray, $data);
6$jsonData = json_encode($tempArray);
7file_put_contents('results.json', $jsonData);
1<?php
2$myfile = fopen("file_name.txt", "w") or die("Unable to open file!");
3$txt = "Hello world\n";
4fwrite($myfile, $txt);
5$txt = " Php.\n";
6fwrite($myfile, $txt);
7fclose($myfile);
8?>