zero-perfoliate
zero-perfoliate

Author Topic: Write to a file, then read, but no result until page refresh.  (Read 338 times)

Offline cajhne

  • New PHP Members
  • Posts: 3
  • Karma: +0/-0
Write to a file, then read, but no result until page refresh.
« on: January 12, 2009, 03:26:36 AM »
Hey folks. I've been beating my head against this one.
I'm trying to simply write to a file, then immediately read from the file with another function.
My code does write okay, but the immediately following read doesn't seem to notice. :P
Any help would be astounding. Thanks.

Code:

<?php


function appendToFile($text_filename,$new_content)
{
   $this_file = fopen($text_filename,"r");
   if (!$this_file)
   {print("Error: Can not open: $text_filename for read.");}
   else
   {
      $new_content=fread($this_file, filesize($text_filename)).$new_content;
      fclose($this_file);
   }
   $this_file = fopen($text_filename,"w");
   if (!$this_file)
   {print("Error: Can not open: $text_filename for write.");}
   else
   {
      
      fwrite($this_file,trim($new_content));
      fclose($this_file);
   }
   print("<BR><BR>ITEM APPENDED<BR><BR>");
}

function showFile($text_filename)
{
         $this_file = fopen($text_filename,"r");
         if (!$this_file)
         {print("Error: Can not open: $text_filename for read.");}
         else
         {
            $new_content="";
            print(fread($this_file, filesize($text_filename)));
            fclose($this_file);
         }
return null;
}

appendToFile("test.txt","ITEM!<BR>\n");
showFile("test.txt");
?>