Page 1 of 1

append to beginning of file

Posted: Fri Mar 17, 2006 2:11 pm
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

Posted: Fri Mar 17, 2006 2:43 pm
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

Posted: Fri Mar 17, 2006 3:13 pm
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