Jump to: navigation, search

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


  1. PHP File Linux vs. Windows
  2. PHP File Information
  3. PHP File Create
  4. PHP File Close
  5. PHP File Read
  6. PHP File Read Reverse
  1. PHP File Write
  2. PHP File Write to end or append to
  3. PHP File Write at Beginning
  4. PHP File Write at specific location
  5. PHP File Truncate
  6. PHP File Replace a specific word
  1. PHP File Replace a specific word with associative array
  2. PHP File Copy File
  3. PHP File Copy Reverse
  4. PHP File Search within file
  5. PHP File Delete File
  6. PHP File Template
  1. PHP File Email Template
  2. PHP File Template with MySQL
  3. PHP File Access with MySQL
Navigation
Web
SQL
MISC
References