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

Write to a file from PHP

By Angela Bradley, About.com

2 of 3

Rewrite Data

If we were to run this very same thing again only using different data, it would erase all of our current data, and replace it with the new data. Here is an example:

<?php
$File = "YourFile.txt";
$Handle = fopen($File, 'w');
$Data = "John Henry\n";
fwrite($Handle, $Data);
$Data = "Abigail Yearwood\n";
fwrite($Handle, $Data);
print "Data Written";
fclose($Handle);
?>

The file we created, YourFile.txt, now contains this data:
John Henry
Abigail Yearwood

Index: Write to a file from PHP

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

2 of 3

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.