Writing data to the BEGINNING of a flat file???

Discussion in 'PHP' started by dp-user-1, Oct 12, 2006.

  1. #1
    I need this script to write the data to the beginning of the file, rather than the end.

    SCRIPT:
    <html>
    <head>
    <title>Posting</title>
    </head>
    <body>
    <?php 
    $date = $_GET["date"];
    $title = $_GET["title"]; 
    $message = $_GET["message"];
    print("Post submitted.");
    $out = fopen($_SERVER['DOCUMENT_ROOT'].'/blogfile.txt', "a"); 
    if (!$out) { 
    print("Could not append to file"); 
    exit; 
    } 
    fputs ($out,implode,("\n")); 
    fwrite($out,"$date|$title|$message");
    fwrite($out,("\n"));
    fclose($out);
    ?>
    </body>
    </html>
    PHP:
    I know it may not seem logical to you, but it does to me. Thanks for the help.
     
    dp-user-1, Oct 12, 2006 IP
  2. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Change this:
    $out = fopen($_SERVER['DOCUMENT_ROOT'].'/blogfile.txt', "a"); 
    PHP:
    to this:
    $out = fopen($_SERVER['DOCUMENT_ROOT'].'/blogfile.txt', "r+"); 
    PHP:
    Here is the applicable section in the manual for the PHP fopen function.
     
    tflight, Oct 12, 2006 IP
  3. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #3
    But that overwrites the existing data. :(
     
    dp-user-1, Oct 12, 2006 IP
  4. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #4
    <html>
    <head>
    <title>Posting</title>
    </head>
    <body>
    <?php 
    $date = $_GET["date"];
    $title = $_GET["title"]; 
    $message = $_GET["message"];
    print("Post submitted.");
    $file = $_SERVER['DOCUMENT_ROOT'].'/blogfile.txt';
    $out = fopen($file, "r+"); 
    if (!$out) { 
    print("Could not append to file"); 
    exit; 
    } 
    $old_content = fread($out, filesize($file));
    fputs ($out,implode,("\n")); 
    fwrite($out,"$date|$title|$message");
    fwrite($out,("\n"));
    fwrite($out, $old_content);
    fclose($out);
    ?>
    </body>
    </html>
    PHP:
     
    SoKickIt, Oct 12, 2006 IP
  5. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #5
    It works, but not entirely.

    I need:
    NEW DATA
    OLD DATA

    That script makes:
    OLD DATA
    NEW DATA
    OLD DATA
     
    dp-user-1, Oct 12, 2006 IP
  6. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #6
    Try this:

    $out = fopen($file, "w+"); 
    PHP:
     
    SoKickIt, Oct 12, 2006 IP
  7. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Have you tried opening it for appending mode (like the original code) but then using something like:
    fseek($out, 0);
    PHP:
    Which should rewind the pointer to the beginning?
     
    tflight, Oct 12, 2006 IP
  8. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #8
    fseek($out, 0); didn't work.

     
    dp-user-1, Oct 12, 2006 IP
  9. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #9
    I tried that too, but it just replaced the new post each time, if I remember correctly.
     
    dp-user-1, Oct 12, 2006 IP
  10. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #10
    <html>
    <head>
    <title>Posting</title>
    </head>
    <body>
    <?php 
    $date = $_GET["date"];
    $title = $_GET["title"]; 
    $message = $_GET["message"];
    print("Post submitted.");
    $file = $_SERVER['DOCUMENT_ROOT'].'/blogfile.txt';
    $file2 = $_SERVER['DOCUMENT_ROOT'].'/blogfile2.txt';
    $old = fopen($file, "r"); 
    $old_content = fread($old, filesize($file));
    $new_content = fread($new, filesize($file2));
    $new = fopen($file2, "w+");
    fwrite($new, $old_content);
    fclose($new);
    $old = fopen($file, "w");
    fclose($old);
    $old = fopen($file, "a");
    fputs ($old,implode,("\n"));
    fwrite($old,"$date|$title|$message");
    fwrite($old,("\n"));
    fwrite($old, $new_content);
    fclose($old);
    ?>
    </body>
    </html>
    PHP:
    I'm trying to use a new file as a sort of "cache," but it's not working. Any ideas why?
     
    dp-user-1, Oct 12, 2006 IP
  11. pondlife

    pondlife Peon

    Messages:
    898
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I had a similar problem that I solved by using the append (putting the new info in at the end of the file) and then displaying it by reading it back via reversing the array. Array_Reverse function info here: http://uk.php.net/function.array-reverse

    I've changed my code recently but I think it looked like this (reading the file back):
    HTH. :)
     
    pondlife, Oct 12, 2006 IP
  12. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #12
    GOT IT!!!!

    <html>
    <head>
    <title>Posting</title>
    </head>
    <body>
    <?php 
    $date = $_GET["date"];
    $title = $_GET["title"]; 
    $message = $_GET["message"];
    print("Post submitted.");
    $file = $_SERVER['DOCUMENT_ROOT'].'/blogfile.txt';
    $file2 = $_SERVER['DOCUMENT_ROOT'].'/blogfile2.txt';
    {
    //Imports old data
    $old = fopen($file, "r");
    $old_content = fread($old, filesize ($file));
    fclose($old);
    
    //Writes new data
    $new = fopen($file2, "w");
    fputs ($new,implode,("\n"));
    fwrite($new,"$date|$title|$message");
    fwrite($new,("\n"));
    fwrite($new,$old_content);
    fclose($new);
    
    //Imports new data
    $new = fopen($file2, "r");
    $new_content = fread($new, filesize ($file2));
    fclose($new);
    
    //Replaces old data
    $old = fopen($file, "w+");
    fwrite($old,$new_content);
    fclose($old);
    } 
    
    ?>
    </body>
    </html>
    PHP:
     
    dp-user-1, Oct 12, 2006 IP
  13. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #13
    Thanks for all the input, guys!
     
    dp-user-1, Oct 12, 2006 IP
  14. Nickwiz

    Nickwiz Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    If you do the whole write in one. And then close the file; why not:

    
    
       // With file_get_contents()
       $file = 'log.txt';
       $data = date("H:i:s",time())."\tThis is some \n text; foo was here.\n";
       $log  = fopen($file,is_file($file)?"r+":"w+");
       fwrite($log,$data.file_get_contents($file));
       fclose($log);
    
    
    PHP:
    ;or...

    
    
       //With file()
       $file = "log.txt";
       $data = date("H:i:s",time())."\tThis is some \n text; foo was here.\n";
       $log  = fopen($file,is_file($file)?"r+":"w+");
       fwrite($log,$data.implode('',file($file)));
       fclose($log);
    
    
    PHP:


    **

    And from your code in first post;


    
    
    <html>
    <head>
      <title>Posting</title>
    </head>
    <body>
    <?php
    
      $date    = $_GET["date"];
      $title   = $_GET["title"];
      $message = $_GET["message"];
      $bfile   = $_SERVER['DOCUMENT_ROOT'].'/blogfile.txt';
    
      print("Post submitted.");
    
      if(!$out=fopen($bfile,is_file($bfile)?"r+":"w+"))
        die("Could not append to file");
    
      fwrite($out,"$date|$title|$message\n".file_get_contents($bfile));
      fclose($out);
    
    ?>
    </body>
    </html>
    
    
    
    PHP:
     
    Nickwiz, Oct 23, 2006 IP