1. Computing

Write to a file from PHP

By , About.com Guide

3 of 3

Adding To Data
Let's say that we don't want to rewrite over all of our data. Instead we just want to add more names to the end of our list. We would do that by changing our $Handle line. Currently it is set to w which means write only, beginning of file. If we change this to a it will append the file. This means it will write to the end of the file. Here is an example:

 <?php 
 $File = "YourFile.txt"; 
 $Handle = fopen($File, 'a');
 $Data = "Jane Doe\n"; 
 fwrite($Handle, $Data); 
 $Data = "Bilbo Jones\n"; 
 fwrite($Handle, $Data); 
 print "Data Added"; 
 fclose($Handle); 
 ?>

This should add these two names to the end of the file, so our file now contains four names:
John Henry
Abigail Yearwood
Jane Doe
Bilbo Jones

Related Video
Zip File 101
Share a File or Folder in Vista
  1. About.com
  2. Computing
  3. PHP / MySQL
  4. Advanced PHP
  5. Write File - PHP Write File - PHP Write to File

©2013 About.com. All rights reserved.