fwrite to write on top.

Discussion in 'PHP' started by sandeep, Apr 24, 2008.

  1. #1
    Hi,

    I am currently making an RSS creater using fwrite, but there seems to be a problem, the data gets submitted at the end therefore the newest data will not show first.

    Is there anyway I can get the data to be placed at the top of the file.

    Thanks everyone
     
    sandeep, Apr 24, 2008 IP
  2. andrew1056

    andrew1056 Peon

    Messages:
    196
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    hummm.. when using fopen change the opening mode like this:

    
    $handle = fopen("/home/test/public_html/text.txt", "r+");
    
    Code (markup):
    The r+ at the end of that command opens the file for reading and writing and places the pointer at the beginning of the file. That way your new content will be printed first. :)
     
    andrew1056, Apr 24, 2008 IP
  3. sandeep

    sandeep Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    using r+ will remove all the data from the file and rewrite it to what new data needs to be added. I need a way for it to keep the data already in the file but to add more on top instead of the bottom.

    Thanks,

    EDIT: I have figured out a different way of doing this. If anyone wanted to know all I did was write what was already in the file to another file and I used, file_get_contents to use that data and re-append it into the rss file with the new data at the top. then i just used unlink to remove the file.
     
    sandeep, Apr 24, 2008 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    Any modern RSS reader is going to at least allow the reader to organize and order results according to item timestamps.

    You really don't need to bother yourself with the order which items appear in the feed.
     
    joebert, Apr 25, 2008 IP