write info to a txt file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

write info to a txt file

Post by andylyon87 »

basically its dead simple all I want to do is write to a text file! The code is below, the problem isn't that I cant write to the file but why is it not adding the data to the end of the file?

The code is the simplified test Im using!

Code: Select all

<?
function write_file($filename,$newdata) {
          $f=fopen($filename,"w");
          fwrite($f,$newdata);
          fclose($f);  
   }

function append_file($filename,$newdata) {
          $f=fopen($filename,"a");
          fwrite($f,$newdata);
          fclose($f);  
   }
   
function read_file($filename) {
          $f=fopen($filename,"r");
          $data=fread($f,filesize($filename));
          fclose($f);  
          return $data;
   }
write_file("test.txt","This is my initial text\n");
append_file("test.txt","New text added\n");
 $data=read_file("test.txt");

   echo "Data in file : <b>$data</b>";  
  ?>
This returns:

Data in file : This is my initial text New text added

Surely there is meant to be a line break between "text" and "New" !
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

There is, see nl2br()
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

thanks for that. Dunno why I hadnt realised that!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

First off, I suggest you use <?php instead of <?

I have seen posts where that caused problems. :)

Maybe replace line breaks with <br /> tags:?:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

PHPyoungster wrote:Maybe replace line breaks with <br /> tags:?:
Thats what nl2br() does
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

I say, good show. :wink:

Cool.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

There's nothing wrong with the text file, and depending on your purposes you may not want the <br />'s there. If you view the source of the output, you will see that the line break is there. If you're intending on viewing the text files (not in a web browser) don't use nl2br(). If you're intending on viewing them inside of a web page, use nl2br().
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Cha.
Post Reply