I am trying to have a page always write to the begining of a file, like append but to the start of it instead of from the end.
this next piece of code appends to the end of the file:
PHP Code:
$fh = fopen($LogFile, 'a') or die("can't open file");
$Mensaje = "<b>".$_GET["Nick"]."</b> at ".date('d\/m\/Y H:i')." wrote:\"".$_POST["Mensaje"]."\"<br>";
//rewind($fh);
fseek($fh, 0);
fwrite($fh, $Mensaje);
fclose($fh);
as you can see I already tried the rewind() but didnt succeed, and also failed when tried the fseek() so Im running out of ideas...
It is a flat file and it is writing to it, just not where I want it...
ideas?
thanks guys
Please! use the PHP tags! or at least CODE tags
(Code posted may work )
--
Checking PHP Manual for fopen(), an 'a' mode opens a file for writing only and places the file pointer at the end of the file. Try using fopen()'s other mode like 'w' or 'r+' which places the file pointer at the beginning of the file.
You can only either append to the end of a file, or overwrite the entire file; you cannot prepend to it. Basically you need to read in the entire contents of the file (such as with file_get_contents()), then open the file in "w" mode, output the new data, then output the file contents you previously read into memory.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
open a temp file and write in the data you want to pre append
open the file you want pre appended and then append the data that is in the old file,
delete the old file,
rename the temp file to the name of the old file you deleted...
hey presto! preappending simplified.
If you use a database, you make things even easier as you only need to run a routine to extract in the order you want.
Bookmarks