You can't use r+ because that overwrites starting at the beginning of your file, it doesn't push what is there forward to make space for your new entry (I was under that assumption too, the first time I read that part of the manual).
To do what you need to, I think (I'm a beginner too) you have to have your data into an array so that you can reverse their order when you display it... In other words the oldest entry will be last in the actual file but when you pull the information from the file you show the oldest first, by reversing the array...
I have an example here taken from a guestbook file that does this:
if ($action=="view"){
echo $viewguestbook;
// When there is no data file, create a new one and say there are no entries.
if (!file_exists($filelocation)) {
$newfile = fopen($filelocation,"w+");
fclose($newfile);
echo "There are no entries yet";
}
// Open the datafile and read the content
$newfile = fopen($filelocation,"r");
$content = fread($newfile, filesize($filelocation));
fclose($newfile);
// Remove the slashes PHP automatically puts before special characters
$content=stripslashes($content);
// Put the entries into the array lines
$lines = explode("%",$content);
// Define and fill the reverse array (showing the last entry first)
$rev=array();
$f=0;
for ($i=sizeof($lines)-1;$i>=1;$i--){
$rev[$f]=$lines[$i];
$f++;
}
$lines=$rev;
// Display all the entries of the guestbook
while(list($key)= each ($lines)){
/*
/ split the data lines into a user array
/ user[0] is the name, [1] is the email, [2] is the url [3] is the date and [4] the message
*/
$countryadd="";
$urladd="";
$mailadd="";
$user = explode("#",$lines[$key]);
// if there is a country, display a country
if ($user[6] != ""){$countryadd="".$user[6]." ";}
// if there is an email, display an email link
if ($user[2] != ""){$mailadd="<a href=\"mailto:".$user[2]."\"><img src=\"/images/icons/email_@.gif\" border=0 align=\"absmiddle\" alt=\"[email]\"></a> ";}
// if there is a url, display a link
if ($user[3] != ""){
// if people were dumb enough not to add [url]http://[/url] do so
if (substr($user[3],0,7)!= "http://"){
$user[3]="http://".$user[3];
}
$urladd="<a href=\"".$user[3]."\" target=\"new\"><img src=\"/images/icons/world.gif\" border=0 align=\"absmiddle\" alt=\"[website]\"></a> ";
}
// Display the entries, here you can tweak HTML
echo "
<dl><dt><b>$user[1]</b> $mailadd $urladd $countryadd ($user[4])<BR><BR>
</dt><dd>$user[5]</dd>\n</dl><CENTER><hr width=\"95%\"></CENTER><BR>";
}
}
Since I have the same problem right now, if somebody has a better solution please let us know!