fopen with the 'r+' overwrites part of file

Discussion in 'PHP' started by Colbyt, Jul 25, 2007.

  1. #1
    I an trying to change a script that writes to a txt file from the 'a' attribute to 'r+'. It is tending to overwrite part of the existing file when I do that. I want to append the new stuff to top of the file. Pretty darn close to char for char is what the overwrite appears to be.

    Have I misunderstood how this works?

    If I have, is there someway to make the last appear first when the file is read for viewing?
     
    Colbyt, Jul 25, 2007 IP
  2. ivenms

    ivenms Peon

    Messages:
    334
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can use "a+" to avoid over writing.
     
    ivenms, Jul 25, 2007 IP
  3. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #3
    Yeah, cause 'r+' writes over the whole file. Starting from the begining of that file.

    'a+' starts at the end of what was last wrote in the file and continues from there.

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

    Look at the table and tell me which one is best for you to use?
     
    exodus, Jul 25, 2007 IP
    Colbyt likes this.
  4. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #4
    So overwriting is what happens with 'r+'.

    I really want the new information at the top of the page, not at the bottom.

    Anyone have a suggestion?
     
    Colbyt, Jul 25, 2007 IP
  5. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #5
    Read in the information into a variable. Then add the information to the beginning of the variable and then write it back out. (possibly use arrays to store the information). Redundant way of doing it, but that is the only way around it that I can think of. I would make a function that does it so I can just supply the variables instead of having to write it out each time it is used.




    __________________
    Give [​IMG] for good answers.
     
    exodus, Jul 25, 2007 IP