Adding data to the beginning of a flatfile db - help please

  • 14 years ago

    Hi there

    Apologies for a very long post....

    I am a PHP newbie, attempting to create a PHP script which will enable me to post information to a flatfile db (pipe delimited plain text file) which will then be picked up by another PHP script and used as the source for my RSS news feed.

    I have adapted a PHP script which writes to a flatfile db and I have converted it into a script which allows me to add, edit and delete articles, and store them in pipe delimited format in the flatfile db.

    The problem that then arose (as you will already have guessed) is that new articles are added to the end of the db, whereas I want them at the top so that new articles appear first in the news feed.

    I have tried and failed to get the PHP newsfeed generator to read the lines in the flatfile db in reverse order.

    So I went back to the PHP script which manages the db file, and hit on the idea of implementing the following sequence of events, to take place when I click on the 'submit' button in the 'add article' form:

    1. The contents of the db (news.dat) are copied to a secondary file (news2.dat), overwriting whatever was already there. This bit works - woohoo!

    2. The data entered in the form is then written to the db (news.dat) in write mode, overwriting what was already there (news.dat now consists only of the latest article). This also works - woohoo again!

    3. The data in news2.dat is then written to news.dat in append mode, which should achieve the desired result (new article appears first, old articles follow in original order) . . . except that this bit doesn't work - d'oh!!

    I'm well out of my depth here, cutting / pasting / adapting bits of code from elsewhere, reading guidance about fopen, fwrite etc on various sites but without really getting an understanding of how to structure a long sequence of code - help would be much appreciated please.

    Here's the segment of code in question:

        if($_GET['action'] == "submit") {
        add();

        $primary = "news.dat";
        $secondary = "news2.dat";
        copy($primary, $secondary); // copies news.dat to news2.dat

        $fp = fopen ("news.dat", "w");

        [various fwrite operations here - code snipped to reduce length of post on forum]

        $news2 = 'news2.dat';
        $addendum = fopen ($news2, "r") or die ("could not open news2");
        $data = fread ($addendum, filesize($news2))  or die ("could not read news2")
        ;
        fwrite ($fp, $data) or die ("could not append news2 to news");
        fclose ($news2);
        fclose ($fp);

        print "Article has been added successfully";
        }

    The above is falling over and giving me the "could not open news2" message. I tested the final chunk as a standalone script and it dutifully pasted the content of news2.dat onto the end of news.dat - but as a part of the above routine, it fails.

    Any clues folks?

    TIA,

    Steve





















































Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I invented the term Object-Oriented, and I can tell you I did not have C++ in mind.” - Alan Kay