The following code shows an example of Appending a Text File in PHP.

In order to append the text, we need to open a file in append mode represented by ‘a’ as the second parameter of the fopen() function. In case, the file doesn’t exist, fopen() creates the file with the specified name. After that write some data to the file. Then call the fputs() function to append a line of text in the file. Finally, call the fclose() function to close the file.

<?php
  $f=fopen('atextfile.txt','a');
  $data="some large text!!! some large text!!! some large text!!! some large text!!! some large text!!! some large text!!! 
some large text!!! some large text!!! some large text!!! some large text!!! some large text!!! some large text!!! some large text!!! some large text!!! some large text!!! some large text!!! some large text!!! 
some large text!!! some large text!!! some large text!!! some large text!!! some large text!!! some large text!!! ";
 fwrite($f, $data);
 $text="Inserted Text......";
 fputs($f, $text);
 fclose($f);
?>

Output

Example of Inserting Text by Appending a Text File in PHP
Example of Inserting Text by Appending a Text File in PHP

Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

programmingempire

princites.com