Created
February 23, 2012 19:47
-
-
Save ain/1894692 to your computer and use it in GitHub Desktop.
tail functionality in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// full path to text file | |
define("TEXT_FILE", "/home/www/default-error.log"); | |
// number of lines to read from the end of file | |
define("LINES_COUNT", 10); | |
function read_file($file, $lines) { | |
//global $fsize; | |
$handle = fopen($file, "r"); | |
$linecounter = $lines; | |
$pos = -2; | |
$beginning = false; | |
$text = array(); | |
while ($linecounter > 0) { | |
$t = " "; | |
while ($t != "\n") { | |
if(fseek($handle, $pos, SEEK_END) == -1) { | |
$beginning = true; | |
break; | |
} | |
$t = fgetc($handle); | |
$pos --; | |
} | |
$linecounter --; | |
if ($beginning) { | |
rewind($handle); | |
} | |
$text[$lines-$linecounter-1] = fgets($handle); | |
if ($beginning) break; | |
} | |
fclose ($handle); | |
return array_reverse($text); | |
} | |
$fsize = round(filesize(TEXT_FILE)/1024/1024,2); | |
echo "<strong>".TEXT_FILE."</strong>\n\n"; | |
echo "File size is {$fsize} megabytes\n\n"; | |
echo "Last ".LINES_COUNT." lines of the file:\n\n"; | |
$lines = read_file(TEXT_FILE, LINES_COUNT); | |
foreach ($lines as $line) { | |
echo $line; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wat if i dont specify the number of line in define("LINES_COUNT", 10); instead just get whole file