How to Create or open a file and write text into PHP
Posted On at by milanFollowing code will help us in creating a file a write text into it, if file with specified name already exists if will overwrite the file data.
$fh = fopen('fileName.txt', 'w') or die("can't open file");
$text = "this is line one\n";
fwrite($fh, $text); //write first time to file
$text = "this is line 2\n";
fwrite($fh, $text);//write second time to file
fclose($fh);
?>
Variable $text may contain many lines.
Remember if file already exists this will remove all old data, to add data to file without removing old data see append data to a file