PHP File Append
From w3cyberlearnings
Contents |
PHP Append to the end of a file
Write to the end of a file or append to a file.
Syntax fopen
a: append to a file
filehandle = fopen(filename','a');
File Content(test1.txt)
A peaceful person makes a peaceful heart.
Example 1
<?php $file_handler = fopen('test1.txt', 'a') or exit('unable to read the file'); fwrite($file_handler, "Lovely day every day!"); fclose($file_handler); ?>
Output
A peaceful person makes a peaceful heart. Lovely day every day!
Example 2
<?php $file_handler = fopen('test1.txt', 'a') or exit('unable to read the file'); $peace = array('A peaceful heart', 'A peaceful world', 'A peaceful person', 'A peaceful community'); foreach ($peace as $content) { fwrite($file_handler, "{$content}\n"); } fclose($file_handler); ?>
Output
A peaceful person makes a peaceful heart. A peaceful heart A peaceful world A peaceful person A peaceful community
Related Links
- PHP File Write
- PHP File Write to end or append to
- PHP File Write at Beginning
- PHP File Write at specific location
- PHP File Truncate
- PHP File Replace a specific word