fwrite Add to top?

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
Darkside
Forum Commoner
Posts: 43
Joined: Wed Oct 30, 2002 4:18 pm

fwrite Add to top?

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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);
Darkside
Forum Commoner
Posts: 43
Joined: Wed Oct 30, 2002 4:18 pm

Thanks

Post 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.
Post Reply