I´m testing file creation in PHP. I´m using a comment.php file that has an HTML form and points to list.php file that does all the job, actually.
This is what I have at list.php:
define(ARCHIVO, 'comentarios.txt');
if (!empty($_POST['nombre']) && !empty($_POST['comentario'])){
$nombre=$_POST['nombre'];
$comentario=$_POST['comentario'];
$fp=fopen(ARCHIVO, 'a');
rewind($fp);
fwrite($fp, '<strong>Nombre: </strong>'.$nombre."<br>\r\n".
'<strong>Comentario: </strong>'.$comentario."<br>\r\n".
'<strong>Fecha: </strong>'.
date("d-m-Y")."<br>\r\n<hr>"
);
fclose($fp);
}
$fp=fopen(ARCHIVO, 'r');
fpassthru($fp);
fclose($fp);
Now, my code added all comments one after the other, and I wanted to be the opposite: The newest ones at the top. So I´ve added to this code the rewind($fp);
line, hoping that it would get the file pointer back at the begining of the txt file before adding more stuff.
It doesn´t work, I mean, it still paste the new stuff at the bottom instead of at the top.
What am I doing wrong?