Reading a file backwards.

[b][red]This message was edited by d00zer at 2003-5-31 9:23:33[/red][/b][hr]
[b][red]This message was edited by d00zer at 2003-5-31 9:19:11[/red][/b][hr]
I just starting coding PHP, I begin with a simple hit counter. And now Im up to coding a simple guestbook. The script gets the data from a form-tag, and writes it to a text file, then the script prints out the data in the text file. Now I want the script to set the file pointer at the end and read it from the end to the start. And print it like that to. How do I do that?
So the messages you write comes first, in the top. Not last.

The script:


$guestbook = "guestbook.txt";
$timestamp = date("l dS of F Y h:i:s A");

if($nick)
{
if($ghandle = (fopen($guestbook,"a")))
{
fwrite($ghandle,$timestamp."");
fwrite($ghandle,"nick: ".$nick."");
fwrite($ghandle,$comment."
");
fclose($ghandle);

} else { echo "Error when opening ".$guestbook; }
}

if($ghandle = (fopen($guestbook,"r")))
{
$contents = fread($ghandle, filesize ($guestbook));
fclose($ghandle);
echo ($contents);

} else { echo "Error when opening ".$guestbook; }


The script: http://hjalmar.jamtland.net/~tobiasp/doozer/guestbook.php


//d00zer
n e v e r s l o w d o w n




Comments

  • you can set the filepointer at the end of file by using A+ but im not sure it will read backwards. But, you can solve it:

    just read the file from START to END and put the contents in some array. Then simply loop through the array from END to START. Very easy ;-)


    : [b][red]This message was edited by d00zer at 2003-5-31 9:23:33[/red][/b][hr]
    : [b][red]This message was edited by d00zer at 2003-5-31 9:19:11[/red][/b][hr]
    : I just starting coding PHP, I begin with a simple hit counter. And now Im up to coding a simple guestbook. The script gets the data from a form-tag, and writes it to a text file, then the script prints out the data in the text file. Now I want the script to set the file pointer at the end and read it from the end to the start. And print it like that to. How do I do that?
    : So the messages you write comes first, in the top. Not last.
    :
    : The script:
    :
    :
    : $guestbook = "guestbook.txt";
    : $timestamp = date("l dS of F Y h:i:s A");
    :
    : if($nick)
    : {
    : if($ghandle = (fopen($guestbook,"a")))
    : {
    : fwrite($ghandle,$timestamp."");
    : fwrite($ghandle,"nick: ".$nick."");
    : fwrite($ghandle,$comment."
    ");
    : fclose($ghandle);
    :
    : } else { echo "Error when opening ".$guestbook; }
    : }
    :
    : if($ghandle = (fopen($guestbook,"r")))
    : {
    : $contents = fread($ghandle, filesize ($guestbook));
    : fclose($ghandle);
    : echo ($contents);
    :
    : } else { echo "Error when opening ".$guestbook; }
    :
    :
    : The script: http://hjalmar.jamtland.net/~tobiasp/doozer/guestbook.php
    :
    :
    : //d00zer
    : n e v e r s l o w d o w n
    :
    :
    :
    :
    :
    [size=5][italic][blue]Dar[RED]Q[/RED][/blue][/italic][/size]
    IP: 62.194.168.177

  • you could do what i'd do... don't read backwards but write the file differently

    what i did was when it was time to sign the guestbook.. I'd open the guestbook for reading, read all the contents into a variable. open the file for writing, write the data in the forms, and then write the variable that held everything before... hence the newest signature goes on top. I think you should be able to do what i said without any source code. Heh.. this is the first time i've tried to help someone on here ;-)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion