append to beginning of file

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
thebabyseal
Forum Newbie
Posts: 2
Joined: Fri Mar 17, 2006 2:07 pm

append to beginning of file

Post by thebabyseal »

Hi everyone,

I have a php script that takes input from an html form and puts it in a text file. Basically its kind of like a web-log so i want the newest entry to be at the TOp of the text file, not at the bottom. Heres my source:

Code: Select all

<?php

$formating = "<p>testformat<p>";

// Open the file
$fp = fopen("test.html", "a+");

// Write the data to the file
rewind($fp);
fwrite($fp, $formatting);
fwrite($fp, $newsupdate);
// Close the file
fclose($fp);


?>
As you can see its just a simple script. Ive tried to use the rewind command but it still appeneds to the end of the file. I must not be using it correctly.

Thanks for any help! andy
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

The append flag automatically appends to the end of the file. Is it important to add onto the beginning? You could read in the last N lines then reverse their order to mimic that? The only way to push strings infront of a file is to read in the file, concate your string to it and write the file back
thebabyseal
Forum Newbie
Posts: 2
Joined: Fri Mar 17, 2006 2:07 pm

Post by thebabyseal »

I did think about trying to read then file and then reserve their order, but i didnt know where to start sicne im a php novice. How would i go about doing something like that? Would it be complex?

thanks andy
Post Reply