PHPBuilder - Tail

RSS Twitter
Snippets Other


by: Tim Thorpe
December 1, 2001

Version: 0.5

Type: Function

Category: Other

License: BSD License

Description: Here's an include file that gives your scripts the mystical powers of the tail command. I give it a version of 0.5 because even though it works fine I'm sure someone will find a way to make it better. :)<?PHP
* - A PHP rough analogue of the *nix tail command
*	written by Tim Thorpe (Majik Sheff)
*	this code is distributed under the terms of the FreeBSD license, for details refer to
*	questions, comments, compliments, donations, ridicule, rants, threats, flames, and friendly hellos can
*	be directed to:
*	Hope it's useful to you!  (if it is, let me know)
*	syntax:
* bool tail (string FileName, boolean Follow, string CallOut, string LockFile, int RollBack, int SleepTime, int BufferSize)
*	Example usage:
*	include("");
*	tail("/var/log/logfile", TRUE, 'echo_something($buffer);', "/var/tmp/tail.lock", 1024, 5, 1024);
*	function echo_something($text) {
*		echo $text;
*	}
*	Will cause the script to follow and echo /var/log/logfile using the default rollback, buffer, and sleep values
*	(note the extra commas at the end and the single quotes around the third argument).

function tail($inputFile,  $followFlag, $callOut, $lockFile, $rollBack, $sleepTime, $bufferSize) {
	if (!file_exists($inputFile)) return(FALSE);		//first see if there's a point in running
	if ($followFlag) touch($lockFile);		//set the lock file if you want to continue tailing (delete this file to kill a tail in progress)
	$fileInputFile = fopen($inputFile, "r");		//open the file to tail
	clearstatcache();		//flush the filesystem stat cache
	$inputFileSize = filesize($inputFile);		//get the size of the file
	if ($inputFileSize > $rollBack)  fseek($fileInputFile, $inputFileSize - $rollBack);		//see if the file has grown since the last check
	while (!feof ($fileInputFile)) {		//if it has, roll out the new lines one at a time
		$buffer = fgets($fileInputFile, $bufferSize);		//get a line
		eval($callOut);		//send the line to the callout function (your callout function will need to reference $buffer for the data)
	while (file_exists($lockFile)) {		//keep looping as long as the lock file exists
		$oldFileSize = $inputFileSize;		//note the current size value
		$oldFilePosition = ftell($fileInputFile);		//note the current pointer position
		clearstatcache();		//flush the cache again
		$inputFileSize = filesize($inputFile);		//get the current file size
		if ($inputFileSize != $oldFileSize) {		//see if it's different
			if ($inputFileSize < $oldFileSize) {		//see if it shrank (or was blanked)
				fclose($fileInputFile);		//close the file and...
				$fileInputFile = fopen($inputFile, "r");		//reopen it just in case it was rotated out (log files, etc)
				fseek($fileInputFile, $inputFileSize);		//move to the end of the file and carry on...
			} else {		//if it didn't shrink, then it grew and...
				fseek($fileInputFile, $oldFilePosition);		//we need to jump to where we left off
			while (!feof ($fileInputFile)) {		//roll through the new lines
				$buffer = fgets($fileInputFile, $bufferSize);		//get a line
				eval($callOut);		//send it to the callout
		sleep($sleepTime);		//sleep
	fclose($fileInputFile);		//clean up

Comment and Contribute

Your comment has been submitted and is pending approval.

Tim Thorpe



(Maximum characters: 1200). You have characters left.