Page 1 of 1

Append to top of file???

Posted: Sat Aug 09, 2003 6:24 pm
by Shiro
I was wondering if there was a way to append stuff to the top of the file instead of the bottom

Code: Select all

<html>
  <head>
    <title>Whatever</title>
  </head>
  <body>
    <table align=center border=1 bordercolor=#000069 cellpadding=2 cellspacing=0>
      <?php
      $username = $_POST&#1111;'PosterName'];
      $header = $_POST&#1111;'header'];
      $pass = $_POST&#1111;'Password'];
      $comments = $_POST&#1111;'Comments'];
      if(($username == 'I2' && $pass == 'I321') || ($username == '12345' && $pass == '12345') || ($username == '23o' && $pass == 'e23t'))&#123;
	      $fp = fopen("comments.cs", "a+");
	      if($fp)&#123;
		      $curDate = date("F jS, Y");
		      $acomment&#1111;0] = "<table cellspacing='0' cellpadding='0' width='350' class='Header'>";
		      $acomment&#1111;1] = "<tr><td width='13' class='left'></td>";
		      $acomment&#1111;2] = "<td height='33' valign='center' class='main'><p align='center'> >> $header ($curDate)</td>";
		      $acomment&#1111;3] = "<td width='13' class='right'></td></tr></table>";
		      $acomment&#1111;4] = "<table border='1' cellspacing='0' cellpadding='3' style='border-collapse: collapse' bordercolor='#AAAAAA' id='AutoNumber1' width='330'>";
		      $acomment&#1111;5] = "<tr><td bgcolor='#DDDDDD'><font size='2' color='#000000'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$comments";
		      $acomment&#1111;6] = "<p align='right'>-$username</font></td></tr></table><br><br>";
		      for($i = 0; $i < 7; $i++) fputs($fp, stripslashes($acomment&#1111;$i]), strLen($acomment&#1111;$i]));
		      fclose($fp);
		  &#125;else&#123;
			  echo "<tr><td>Could not open file, comments.cs</td></tr>";
		  &#125;
	  &#125;else&#123;
		  echo "<tr><td>Invalid UserName and/or Password.</td></tr>";
	  &#125;
	  ?>
      <tr><td>Click <a href="http://www.planettribes.com/daf/habitat_Entity/">here</a> to return to main page.</td></tr>
    </table>
  </body>
</html>

Posted: Sat Aug 09, 2003 7:18 pm
by skateis2s
not apend to the top, but there is a way to write to the top of a file by saving the old and than writing to it

function write_beg($filename, $data) {
$handle = fopen($filename, "r");
$old_content = fread($handle, filesize ($filename));
fclose($handle);

$final_content = $data.$old_content;

$handle2 = fopen($filename, "w");
$finalwrite = fwrite($handle2, $final_content);
fclose($handle2);
}

uhh

Posted: Sat Aug 09, 2003 7:41 pm
by Shiro
uhh i dont thinkn that will work the way i have mine

Posted: Sat Aug 09, 2003 7:54 pm
by JAM

Code: Select all

<?php
...
      if(($username == 'I2' && $pass == 'I321') || ($username == '12345' && $pass == '12345') || ($username == '23o' && $pass == 'e23t')){ 

        // read old file...
        $handle = fopen("comments.cs", "r"); 
        $old_content = fread($handle, filesize ("comments.cs")); 
        fclose($handle); 

         $fp = fopen("comments.cs", "a+"); 
         if($fp){ 
            $curDate = date("F jS, Y"); 
            $acomment[0] = "<table cellspacing='0' cellpadding='0' width='350' class='Header'>"; 
            $acomment[1] = "<tr><td width='13' class='left'></td>"; 
            $acomment[2] = "<td height='33' valign='center' class='main'><p align='center'> >> $header ($curDate)</td>"; 
            $acomment[3] = "<td width='13' class='right'></td></tr></table>"; 
            $acomment[4] = "<table border='1' cellspacing='0' cellpadding='3' style='border-collapse: collapse' bordercolor='#AAAAAA' id='AutoNumber1' width='330'>"; 
            $acomment[5] = "<tr><td bgcolor='#DDDDDD'><font size='2' color='#000000'>     $comments"; 
            $acomment[6] = "<p align='right'>-$username</font></td></tr></table><br><br>";

            for($i = 0; $i < 7; $i++) fputs($fp, stripslashes($acomment[$i]), strLen($acomment[$i])); 
            // insert it after above...
            fwrite($fp, $old_content);
            fclose($fp); 
        }else{ 
...
?>
Added some code & comments. Not tested, but the basics are there. Might work after adding some /n's here and there.

Posted: Sat Aug 09, 2003 9:40 pm
by Kriek
You mean prepend to the top of the file ;)

Sure using file_put_contents()

Code: Select all

<?php
    $filename = 'comments.cs';
    $text = 'This line goes at the top of the file'."\n";
    $buff = file_get_contents($filename);
    if (!@file_put_contents($filename, $text.$buff)) {
        echo "Unable to write to file $filename";
    } else {
        echo "Success writing to $filename";
    }
?>

Thanks

Posted: Sat Aug 09, 2003 9:48 pm
by Shiro
it works thanks just need to replace r with a w but it works :D thanks

Cool

Posted: Sun Aug 10, 2003 1:33 am
by Master Jakkal
It's funny that I happened to be working on the same thing and was having the same type of problem when bumped into this post.
No more problems!
Cool

Posted: Sun Aug 10, 2003 12:55 pm
by macewan
Kriek - thanks for the phpmeet link. =)