PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Shiro
Forum Newbie
Posts: 12 Joined: Sat Aug 09, 2003 7:08 am
Post
by Shiro » Sat Aug 09, 2003 6:24 pm
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ї'PosterName'];
$header = $_POSTї'header'];
$pass = $_POSTї'Password'];
$comments = $_POSTї'Comments'];
if(($username == 'I2' && $pass == 'I321') || ($username == '12345' && $pass == '12345') || ($username == '23o' && $pass == 'e23t')){
$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]));
fclose($fp);
}else{
echo "<tr><td>Could not open file, comments.cs</td></tr>";
}
}else{
echo "<tr><td>Invalid UserName and/or Password.</td></tr>";
}
?>
<tr><td>Click <a href="http://www.planettribes.com/daf/habitat_Entity/">here</a> to return to main page.</td></tr>
</table>
</body>
</html>
skateis2s
Forum Commoner
Posts: 37 Joined: Fri Aug 08, 2003 7:22 am
Location: colorado
Post
by skateis2s » Sat Aug 09, 2003 7:18 pm
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);
}
Shiro
Forum Newbie
Posts: 12 Joined: Sat Aug 09, 2003 7:08 am
Post
by Shiro » Sat Aug 09, 2003 7:41 pm
uhh i dont thinkn that will work the way i have mine
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Sat Aug 09, 2003 7:54 pm
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.
Kriek
Forum Contributor
Posts: 238 Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:
Post
by Kriek » Sat Aug 09, 2003 9:40 pm
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";
}
?>
Last edited by
Kriek on Sat Aug 09, 2003 9:50 pm, edited 3 times in total.
Shiro
Forum Newbie
Posts: 12 Joined: Sat Aug 09, 2003 7:08 am
Post
by Shiro » Sat Aug 09, 2003 9:48 pm
it works thanks just need to replace r with a w but it works
thanks
Master Jakkal
Forum Newbie
Posts: 4 Joined: Tue Jul 15, 2003 12:42 am
Location: Florida, USA
Contact:
Post
by Master Jakkal » Sun Aug 10, 2003 1:33 am
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
macewan
Forum Commoner
Posts: 97 Joined: Mon Jul 07, 2003 8:27 pm
Post
by macewan » Sun Aug 10, 2003 12:55 pm
Kriek - thanks for the phpmeet link. =)