fopen and fwrite r+

From: Date: Sat, 13 Jan 2007 22:32:00 +0000
Subject: fopen and fwrite r+
Groups: php.general 
Request: Send a blank email to php-general+get-247051@lists.php.net to get a copy of this message
I'm collecting data from a form and using it to write to a flat file like so: Quote: //this removes line breaks in an text field box and substitutes double breaks $postbody = str_replace("\r\n", "<br/><br/>",$postbody); $thepost = "$postitle | $postdate | $postbody | $author \r\n"; if ($posttype=="add") { // the r+ flag reads and writes at the top of the file, "a" writes at the bottom $file = fopen("../includes/news.txt", 'r+'); fwrite ($file, "$thepost\r\n"); fclose($file); } else { } I can write to the bottom of the file, no problem, but if I want to put new entries at the top of the file I have problems: the previous entry is partially overwritten and mangled.. like so: (this is the top line from the text file
Site Redesign | Jan 9, 07, 9:31 am | The site has a new look...etc
becomes
new look!...etc
a 20 character post overwrites 22 a 50 character post overwrites 62 61 character post overwrites 64 260 character post overwrites 260 characters.. it seems to be random, somewhat... any ideas?

« previous php.general (#247051) next »