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

Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.