Page 1 of 1

fwrite Add to top?

Posted: Fri Dec 06, 2002 2:06 pm
by Darkside
I'm using this code & a form to post text to a php file.

Code: Select all

<?php $post_data = "posts.php";
$data = fopen($post_data, "a");
$post = stripslashes($post);
$by = stripslashes($by);
fwrite($data, "$post <br><b>Posted by:</b> $by<hr>");
fclose($data);
Header ("Location: index.php?folder=flash&page=wof"); 
?>
I was wondering weather I would be able to write the infomation that I want to post at the start of the file instead on the end of the file..?

I'm using the script above to make my own sort of shoutbox the only problem being is that the last post is always at the bottom.. Is there any way around this with out using. mysq.

Thanks Darkside

Posted: Fri Dec 06, 2002 2:52 pm
by volka
you might read the current content of the file, write the new data and then write back the stored, old content. e.g.

Code: Select all

$content = file($post_data);
$data = fopen($post_data, 'w');
fwrite($data, "$post <br><b>Posted by:</b> $by<hr>");
foreach($content as $line)
    fwrite($data, $line);
fclose($data);

Thanks

Posted: Fri Dec 06, 2002 4:44 pm
by Darkside
Thanks it works wonderfuly now..

I was think you might have been able to do that.. But I was thinking more of copying whats in there and then deleting it then add new post then add the rest but how can you copy something that you delete.. Bla anywayz thanks.