Category Archives: PHP-appending contents to file

PHP – File Write: Appending Data

Using the filename.txt file we created in the File Write lesson , we are going to append on some more data.
PHP Code:
$myFile = “filename.txt”;
$fh = fopen($myFile, ‘a’) or die(“can’t open file”);
$stringData = “laxmi\n”;
fwrite($fh, $stringData);
$stringData “khem\n”;
fwrite($fh, $stringData);
fclose($fh);
You should noticed that the way we write data to the file is exactly the same as in the Write lesson. The only thing that is different is that the file pointer is placed at the end of the file in append mode, so all data is added to the end of the file.
The contents of the file filename.txt would now look like this:
phpfresher
Kisor
laxmi
bikesh