Jump to content

how to add to the first line of a txt file?


brain

Recommended Posts

Hi,

 

I'm trying edit a script called shoutbox and it simply just adds a name and comment to a txt file on a new line and includes it into my webpage via php.

 

My problem is that it adds the new comment to the bottom... I need it to add that comment to the top in a accending order.

 

 $lines = file($file_path);

$handle = file_connect($file_path, 'a');

if(@fwrite($handle, $message['name']." | ".$message['url']." | ".$message['message']."\n")) {

 

I've tried w+  but it erases the previous comments added to the data.txt file.

Does anyone know how I would add a line to the top of the data.txt file without first erasing it?

Link to comment
Share on other sites

dont do that... i've tried it and all it does is that it overwrites.. so you have to add the new shout to bottom of the file, and then when you read it you have to reverse the array...

 

this is the code i use to write to the file:

<?php

$phps_sbname = $_REQUEST['sbname'];
$phps_sburl = $_REQUEST['sburl'];
$phps_sbmessage = $_REQUEST['sbmsg'];


if ($phps_sbname == '') { echo 'Please enter a name'; }
elseif ($phps_sbmessage == '') { echo 'Please enter a message'; }
else {	

	if ($phps_sburl == '') {
		$phps_newshout = "\n<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td bgcolor=#FFFFFF><strong>" . $phps_sbname . ":</strong> " . $phps_sbmessage . "</td></tr><tr><td bgcolor=#999999 height=1></td></tr></table>";
	}
	elseif ($phps_sburl != '') {
		$phps_newshout = "\n<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td bgcolor=#FFFFFF><strong><a href=" . $phps_sburl . " target=_blank>" . $phps_sbname . "</a>:</strong> 	" . $phps_sbmessage . "</td></tr><tr><td bgcolor=#999999 height=1></td></tr></table>";
	}

	$phps_sbfh = fopen("filename.txt", "ab");
	fwrite($phps_sbfh,$phps_newshout);
	fclose($phps_sbfh);
	Header("Location: URL");
}
?>

 

this is the file that reads the file:

<?php

$phps_sbfh = @fopen('filename.txt', "r");
if ($phps_sbfh) {
   	while (!feof($phps_sbfh)) {
       $phps_sblines[] = fgets($phps_sbfh, 4096);
   	}
   	fclose($phps_sbfh);
   	$phps_sbresult = array_reverse($phps_sblines);
	$phps_sbresult = array_slice($phps_sbresult, 0, 20);
	foreach($phps_sbresult as $phps_shout)
	echo $phps_shout;
}

?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.