Forum Moderators: coopster
I am now using this, just hacked it together using some stuff that was lying around.
params passed are
$_GET['logname'] = this is generated by a read dir type script and of the form filename.ext
$_GET['lines'] = number of lines you want to see
$logname = $_GET['logname'];
$logdir = '/path/to/logfiles/';
$buffer = '';
$lines = false;
if (isset($_GET['lines'])) $lines = $_GET['lines'];
// check to see if they want the full log or just the last x lines
if (!$lines) {
$buffer = file_get_contents($logdir . $logname);
echo '<textarea cols="120" rows="40" name="logdata" wrap="off">';
echo $buffer;
echo '</textarea>';
} else {
$fp = fopen($logdir . $logname,'r');
if ($fp) {
$linesarr = array();
while (!feof($fp)) {
$buffer = fgets($fp);
$linesarr[] = $buffer;
if (count($linesarr) > $lines + 1) array_shift($linesarr);
}
fclose($fp);
// now display what is in the array
echo '<textarea cols="150" rows="21" name="logdata" wrap="off">';
$counter = 0;
while (isset($linesarr[$counter])) {
echo $linesarr[$counter];
$counter++;
}
echo '</textarea>';
} else {
echo '<p>',$logdir,$logname,' could not be opened.';
die();
}
}
there is no input/variable verification on this as it is only used by me and there are 3 layers of security making sure that it's me, IP being one of them. I'm just playing mostly.
I wanted to limit the amount of data I was keeping in memory, hence the array_shift.
I like it displayed in a textarea so it doesn't wrap
can someone think of something more interesting?
$logname = $_GET['logname'];
$logdir = '/path/to/logfiles/';
$file_abs_path = $logdir . $logname;
$buffer = '';
$lines = false;
if (isset($_GET['lines'])) $lines = $_GET['lines'];
// does the file exist?
if( file_exists($file_abs_path))
{
// check to see if they want the full log or just the last x lines
if (!$lines) {
$buffer = file_get_contents($file_abs_path);
} else {
$commmand = "tail -$lines $file_abs_path";
$buffer = exec($command);
}
echo '<textarea cols="120" rows="40" name="logdata" wrap="off">';
echo $log_output;
echo '</textarea>';
}else {
echo '<p>',$logdir,$logname,' could not be opened.';
die();
}
Since this code requires some non-PHP functionality, I'd call myself a cheater.
I figured I could decide that X number of bytes is always more than you want and keeps the mem usage down
jezra, that was my initial plan but for some reason I didn't do that, there was a reason, I just can't remember what it was
I also know that the way I did it will slowly take longer and longer as the file gets larger, I am a little interested to see at what point it starts lagging. Though I am also way too lazy to test it. Guess I'll just let it run and see what happens. :)
like I said, this is for me only so it doesn't really matter
I would be interested if anyone else can think of anything interesting.
reading large text/log files is useful and quite a bit of fun