Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2003
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts

    append to top of file and reload

    Using the basic syntax for appending files:

    $filename = "message.txt";
    $fp = fopen($filename, "a") or die("couldn't open $filename");
    flock($fp, LOCK_EX);
    fwrite($fp, "hello, this is testing");
    flock($fp, LOCK_UN);
    fclose($fp);
    //reload to see new message. how?

    The text is appended at the end of whatever is already in message.txt. How can I make it so that it appends at the top of the file instead? So that new messages are at the top.

    Also, what is the code to tell the page to reload?


    thanks very much.
    Last edited by loonatik; 04-07-2003 at 02:15 PM.

  • #2
    New Coder
    Join Date
    Aug 2002
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: append to top of file and reload

    Have a look at this page:

    http://www.php.net/manual/en/function.fopen.php

    It shows the different ways you can open a file, so maybe try it with r+ instead?

    PHP Code:
    $filename "message.txt";
    $fp fopen($filename"r+") or die("couldn't open $filename");
    flock($fpLOCK_EX);
    fwrite($fp"hello, this is testing");
    flock($fpLOCK_UN);
    fclose($fp);
    //reload to see new message. how? 

  • #3
    New Coder
    Join Date
    Mar 2003
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm using this as a message board/guestbook type. So when I use r+ it writes over; and when I use a or a+, it just appends to the end of file. I"ve added the flock($fp, LOCK_EX); but it doesn't apply with a/a+.

    $filename = "message.txt";
    $fp = fopen($filename, "r+") or die("couldn't open $filename");
    flock($fp, LOCK_EX);
    fseek($fp, 0, SEEK_SET);
    fwrite($fp, "hello, this is testing");
    flock($fp, LOCK_UN);
    fclose($fp);

    Is the solution more complex than I think it is?

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,051
    Thanks
    10
    Thanked 94 Times in 92 Posts
    you can't write to the top of the file without overwriting the existing content.

    the normal workaround is to open the file , get the contents add your new content and resave...

    PHP Code:
    <?
    $filename
    ='file.txt';
    $content 'blah';
    $new $content "\n" implode'' file$filename ) );
    $fp =fopen($filename,'w');
    //etc save the $new content
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •