1. Home
  2. Computing & Technology
  3. PHP / MySQL

Write to a file from PHP

By Angela Bradley, About.com

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

3 of 3

Index: Write to a file from PHP

  1. Write To A File
  2. Rewrite Data
  3. Adding To Data

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Advanced PHP
  5. Write File - PHP Write File - PHP Write to File

©2008 About.com, a part of The New York Times Company.

All rights reserved.