PHP Tutorial : Write to a file with fwrite

I showed you earlier in a older post how to open and read text contents from a file. Now it’s time to see how to write to a file using php function called fwrite().

<?php
$theFileToWriteTo = "textFile.txt";
$fileHandle = fopen($theFileToWriteTo, 'w') or die("cannot open the text file");
$textToWrite = "First text to write here\n";
fwrite($fileHandle, $textToWrite);
$textToWrite = "Second line to write here\n";
fwrite($fileHandle, $textToWrite);
fclose($fileHandle)
?>

Note that “\n” means new line!

This entry was posted in Basic PHP and tagged , , , , . Bookmark the permalink.

One Response to PHP Tutorial : Write to a file with fwrite

  1. Pingback: Twitted by freephptutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>