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
|
<!DOCTYPE html> <?php /************************************************** *** ** Title.........: RevFile Class ** Version.......: 1.00 ** Author........: Steve Weet <sweet@weet.demon.co.uk> ** Filename......: class.RevFile.php ** Last changed..: 30th Jan 2004 ** Purpose.......: Allows the display of a file in ** ..............: In reverse order ************************************************** ****/
// Example Usage $file = new RevFile("shouts.html");
while ( ! $file->sof() ) { echo $file->GetLine() ; }
class RevFile {
var $FileName; var $FileHandle; var $FilePos;
function RevFile($filename) {
$this->FileName = $filename;
$this->FileHandle = @fopen($filename, "r") or die("Could not open file $filename\n");
// Find EOF if ( ! (fseek($this->FileHandle, 0, SEEK_END ) == 0 )) die ("Could not find end of file in $filename\n");
// Store file position $this->FilePos = ftell($this->FileHandle);
// Check that file is not empty or doesn;t contain a single newline if ($this->FilePos < 2 ) die ("File is empty\n");
// Position file pointer just before final newline // i.e. Skip EOF $this->FilePos -= 1; } function GetLine() { $pos = $this->FilePos -1; $ch=" "; $line = ""; while ($ch != "\n" && $pos >= 0) { fseek($this->FileHandle, $pos ); $ch = fgetc($this->FileHandle);
// Decrement out pointer and prepend to the line // if we have not hit the new line if ( $ch != "\n" ) { $pos = $pos -1; $line = $ch . $line; } } $this->FilePos = $pos ; return $line . "\n"; }
function sof() { return ($this->FilePos <= 0 ); } } ?>
|