Results 1 to 6 of 6

Thread: .txt Writing

  1. #1

    .txt Writing

    Sorry to bother anyone as this is probably very simple.

    Currently I've got it setup so i can write to a .txt file using PHP through a browser, but I can only get it to write to the bottom of the file.

    Is there any way I can make it write to the top? I'm just trying to have another script read the .txt file into a page and have the latest content of the file on the top.

    Thanks in advance.

  2. #2
    Join Date
    Dec 2004
    When you open the file you select the write mode. 'r' is read, 'w' is write, 'a' is append. In both the read and write modes, the file pointer is placed at the beginning of the file (meaning if you write data it will overwrite starting from the first byte in the file). In append mode, the file pointer is put at the end of the file, so you can add data on to the end of the file. Be careful using the overwrite modes; if you're writing less data than already exists, only bytes you explicitly write will be overwritten, and the rest will remain. To do this, look for the fopen("filename.txt", 'a') in your source and simply change it to fopen("filename.txt", 'w').

    If you want to overwrite the entire file, the best solution is to first create the new file in a temporary location, write the new data to it, then move it overtop of the old file. The reason for this is that the old data will only be overwritten if the PHP process succeeds completely, otherwise the PHP script may fail once the file has been cleared but before the data is fully written. Something like the following would be a good solution.
    PHP Code:

    $dest_file "file.txt";

    if (
    unlink(TMP_FILENAME); // delete any stale temporary files

    $fp fopen(".tmp.yourscript""w"); // open the file and create it
    fwrite($fp"This is some test data.\n"); // replace with your processing and file writing code as normal

  3. #3
    Thank you for replying, however I do not want to overwrite anything. I would like it to be cumulative if possible.

  4. #4
    Join Date
    Jul 2003
    To do a cummulative operation, you can simply do this little "hack":

    PHP Code:
    $new_contents "Line1\nLine2\nLine3";
    $current_contents file("somefile.txt");

    //Open the file in "w" mode, essentially deleting it
    $fp fopen("somefile.txt","w");

    //Write the new content first, then the old content

    //Close it
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  5. #5
    Mr Confused, I believe that a different approach to logic might solve your problem. Instead of reading the file from the very top, why don't you start reading it at the end of the file?
    You can take a look at and use that function to suit your reading needs.
    I believe it's much less overhead than finding a way to write at the top of the file instead the natural way (append new content at the end).

  6. #6
    Thanks to both of you.

Posting Permissions

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