Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding string to top of a TXT file instead of bottom. 2

Status
Not open for further replies.

rossmcl

Programmer
Apr 18, 2000
128
Hi,

This is probably a really easy one...

I use the below code to store some information on a website visitor to a text file.

The thing that is annoying me is that it appends the information to the bottom of the file, and it is now a pain to scroll down to see recent visitors. So I want to append it to the top of the file instead.

What do I have to change in my code to do that?

Thanks
ross

<?php

$filename = &quot;VisitorLog.txt&quot;;
$handle = fopen ($filename, &quot;a+&quot;);
$todaydate = date(&quot;Y-m-d H:i:s&quot;);
$visitorInfo = $HTTP_SERVER_VARS['REMOTE_ADDR'];

fwrite($handle, &quot;\r\n&quot; . $todaydate . ' ' . $visitorInfo);

fclose( $handle );

?>
 
Since this is a sequential process, the only thing possible is to write your record, and then copy the rest of the stuff.

To get what you want you would have to insert into a table, and have a key in reverse order to access the new records.

 
Take into consideration that your file will be geting large, maybe very large. If you want to append to the top of it you always have to read the whole content into a variable and prepend the new record. I'd call this very inefficient. WHen the file grows large you'll hate it because it will take time and your pages will get slower and slower.

Think about displaying the last n lines instead. It's the output presentation that I would work on:

I suggest:
1. Open file for reading
2. Get last n lines into array
3. Resort array and display
 
Add the contents of the existing file to the bottom of a new file :

*notes read and uderstand the benefits of the following PHP functions:
1) flock
2) clearstatcache
3) ignore_user_abort

Code:
<?php
$filename = &quot;VisitorLog.txt&quot;;
$tmpName = &quot;Vistmp.txt&quot;;
if(copy($filename, $tmpName)){
$handle = fopen ($filename, &quot;w+&quot;); // open a new file
$temp=file($tmpName);


$todaydate = date(&quot;Y-m-d H:i:s&quot;);
$visitorInfo = $HTTP_SERVER_VARS['REMOTE_ADDR'];

fwrite($handle, &quot;$todaydate \t $visitorInfo \r&quot;);

	while (list ($line_num, $line) = each ($temp)) {
		fwrite($handle, &quot;$line &quot;);
	}

fclose( $handle );
unlink($tmpName);
}else{
// we failed to copy the file
exit;
}
?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
You could also run a session with
tail -f VisitorLog.txt

which would display the data as it is written.
or use tail with the count parm for the last lines.
 
tail (whatever) will show the last lines of a file, which would still not be in reverse order.

Why run a session, with sys_exec() exec() or cmd(), you can code that ability into a page.



______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
for shorter logs set the number to the number of lines to keep, saves having to manage the logs much:

while (list ($line_num, $line) = each ($temp)) {
if($line_num < 10 ){
fwrite($handle, &quot;$line&quot;);
}

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
of course if you are running this on windows or *nix you need to mod the fwrite to :
fwrite($handle, &quot;$todaydate\t$visitorInfo\n&quot;);

this will make the line terminate correctly so that the file has a number of lines, not 1 with just carriage returns....

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Lots of good answers, particularly the ones about just showing the tail of the file... but if you do want to display full information, I would suggest to keep appending to the end of the file and then just...
Code:
$contents=file($log_file);
$contents=array_reverse($contents);
foreach ($contents as $line) {
  echo $line.'<br />';
}
 
I deliberately avoided array_reverse because if you ignore your log files for any length of timit gets damn slow ... but then maybe thats just my crap PIII 800mhz server.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top