Client file

<html>
<title> file_fwrite.php</title>
<body text="#000080" bgcolor="#F7F0D7">
This is an example of appending text to a file,<br>
you are also restricted to send an empty text area.<br>
The server will send you back to this page again

<p><form method="post" action="file_fwrite.php"><textarea rows="3" name="S1" cols="30"></textarea><br>
<input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
<br>
<body>
</html>

Server side coding


<html>
<title> file_fwrite.php</title>
<body text="#000080" bgcolor="#F7F0D7"><?php
echo "reading existing file---<br/>";
$path1 ="test_write.txt";
$localfile = file_get_contents("test_write.txt");
echo "file size is: ".filesize($path1 ) . ' bytes';
echo "<br/>". $localfile;
if ($_POST['S1']=="")
{
header("Location:file_fwrite.htm");
exit;
}
?>
<?php
//file_fwrite.php
$text = $_POST['S1'];
$fp = fopen("test_write.txt", "ab");
fwrite($fp, "$text\r\n");
fclose($fp);

?>
<?php
echo "<br>reading whole contents ---<br/>";
$path2 ="test_write.txt";
$localfile = file_get_contents("test_write.txt");
echo "<br/>". $localfile;
?>
</body>
</html>

Create a text file as show in the picture below.

Runtime : write some text in the box.

Now, submit.

Note the text appended at the end of the existing code