Forum Moderators: coopster

Message Too Old, No Replies

post problem

post problem

         

ryanc

8:35 pm on Jan 23, 2006 (gmt 0)

10+ Year Member


Ok I have on my site a simple little message board basically like this:
<?php

if ($add=="y"[smilestopper])
{
$date=date("D-M-j"[smilestopper]);
$name=$_POST['name'];
$comment=$_POST['comment'];
$post="<small>$date</small> - <b>$name</b>: <i>$comment</i>";
$fp = fopen("post.txt", "a"[smilestopper]);
fwrite($fp, $post ."<br><br>"[smilestopper]);

} else {

$fp = fopen("post.txt", "r"[smilestopper]);
while (!feof($fp)) {
$buffer = fgets($fp);

echo stripslashes("$buffer"[smilestopper]);

}

fclose($fp);

}

?>

And I wanted to alter it so that new comments would go at the top of the file so I changed the "a" to "r+", but that overwrote weirdly like part of the file would remain but it would overwrite part of it. Then I tried adding rewind($fp); and fseek($fp,0); and still did the same thing.

Anyone have any suggestions?

Thanks,
ryanc

simon2263

9:16 pm on Jan 23, 2006 (gmt 0)

10+ Year Member



You can't really append to the beginning of a file - only to the end. The "r+" mode refers to read AND write mode, so I guess it was writing in place. Anyway, you can only append to the end, since the file table in the operating system will record the start address of the file, and hence appends to the beginning would require shunting up the rest of the file on disk to make space for the new data - highly undesirable!

How many comments might there be? Could you read in the entire file as strings and then reverse them in the php?

Moosetick

10:35 pm on Jan 23, 2006 (gmt 0)

10+ Year Member



Since it is just a test file, just dump it into a string. Then combine them together like...

$file = $newdata . $olddata;

Then you can send the contents of $file to post.txt.

ryanc

1:00 pm on Jan 24, 2006 (gmt 0)

10+ Year Member



yeah I guess that is what I'll have to do.

Thanks,
ryanc