Location: PHPKode > scripts > classtailfile > classtailfile/classtailfile.php
<?php

/*

# ----------------------------------------------
# Program     : classtailfile.php
# Version     : 1.0
# Author      : Marcelle Heymans
# Date        : November 5 2008
# ----------------------------------------------



Usage:

include_once('classtailfile.php');


$mylogfile=new tail("/var/log/messages");
#if startatbegin is not called, getupdates will only return lines that was written to the file after the the instance was created
#$mylogfile->startatbegin();
$mylogfile->sleepfor=1;
while (1)
	{
		print $mylogfile->getupdates();
		
	}
*/

class tail
	{
        private $filename 		= "";
        private $LastPosition 	= 0;
        private $handle;
        public $sleepfor		= 0;
        
        function __construct($filename)
        	{
	        	$this->filename		=$filename;
	        	$this->handle 		=fopen($filename, r);
	        	$this->LastPosition	=filesize($filename);
        	}
        	
        function __destruct()
        	{
	        	fclose($this->handle);
        	}
        	
        function startatbegin()
        	{
	        	$this->LastPosition=0;
        	}
        	
       	function getupdates()
       		{
	       		clearstatcache();
	       		
	       		$filesize=filesize($this->filename);
	       		
	       		if($this->LastPosition < $filesize)
	       			{
		       			fseek($this->handle, $this->LastPosition);
		       			$updates=fgets($this->handle);
		       			$this->LastPosition= ftell($this->handle);
		       			
		       			return $updates;
	       			}
	       		else
	       			{ 
		       			sleep($this->sleepfor);
	       			}
        	}
        	
    }
?>
Return current item: classtailfile