This example below is taken from fwrite manual.
See also
http://php.net/fopen
http://php.net/fwrite
http://php.net/fread
you can open file for 'a' = append,
and it will add new lines WITHOUT overwriting already existing lines
for each fwrite operation, a new line is added
<?php
$filename = 'test.txt';
$line1 = "Add this line1 to the file\n";
$line2 = "Add this line2 to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$fp = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($fp, $line1) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
if (fwrite($fp, $line2) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ( $line1 and $line2 ) to file ($filename)";
fclose($fp);
} else {
echo "The file $filename is not writable";
}
?>
Of course you could make $data = $line1.$line2;
and do only one fwrite operation
$data = $line1.$line2;
$fp = fopen($filename, 'a');
fwrite($fp, $data);
fclose($fp);
echo "Okay $data was written to file.";