-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtail.php
115 lines (79 loc) · 2.1 KB
/
tail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* A script to emulate the UNIX tail function by displaying the last X number
* of lines in a file.
*
* Useful for large files, such as logs, when you want to process lines in PHP
* or write lines to a database.
*
* @author Dan Ruscoe
*/
// The name of the file to read
define('FILE_NAME','file.log');
// The number of lines to display
define('LINES_TO_DISPLAY',10);
if (!is_file(FILE_NAME)) {
die("Not a file - ".FILE_NAME."\n");
}
if (LINES_TO_DISPLAY < 1) {
die("Number of lines to display must be greater than 0\n");
}
if (!$lines = get_lines(FILE_NAME,LINES_TO_DISPLAY)) {
die("Could not get lines from file - ".FILE_NAME."\n");
}
foreach ($lines as $line) {
echo $line;
}
exit;
/**
* Returns an array containing X number of rows from the end of a file.
*
* @param string $filename
* @param int $lines_to_display
* @return array
*/
function get_lines($filename,$lines_to_display) {
// Open the file.
if (!$open_file = fopen($filename,'r')) {
return false;
}
$pointer = -2; // Ignore new line characters at the end of the file
$char = '';
$beginning_of_file = false;
$lines = array();
for ($i=1;$i<=$lines_to_display;$i++) {
if ($beginning_of_file == true) {
continue;
}
/**
* Starting at the end of the file, move the pointer back one
* character at a time until it lands on a new line sequence.
*/
while ($char != "\n") {
// If the beginning of the file is passed
if(fseek($open_file,$pointer,SEEK_END) < 0) {
$beginning_of_file = true;
// Move the pointer to the first character
rewind($open_file);
break;
}
// Subtract one character from the pointer position
$pointer--;
// Move the pointer relative to the end of the file
fseek($open_file,$pointer,SEEK_END);
// Get the current character at the pointer
$char = fgetc($open_file);
}
array_push($lines,fgets($open_file));
// Reset the character.
$char = '';
}
// Close the file.
fclose($open_file);
/**
* Return the array of lines reversed, so they appear in the same
* order they appear in the file.
*/
return array_reverse($lines);
}
?>