PHP reverse reading a text file

how can i read for example 10 last lines from a text file beginning
from last line

 

Sollution 1

$file = array_reverse( file( ‘mine.txt’ ) );
$limit = 10;
for ($i = 0; $i < $limit; $i++ ){
echo $file[$i] . "\n";
}

 

Sollution 2

$handleFile = array_reverse(file(‘mine.txt’ ));
        foreach ($handleFile as $value) {
            $handle.=mod_admin_log_show($value);
}

Sollution 3

<pre>
<?php
$file = ‘test.txt’;
$fp = fopen($file, ‘r’);
if ($fp) {
$lines = array();
while (($line = fgets($fp)) !== false) {
$lines[] = $line;
while (count($lines) > 10)
array_shift($lines);
}
foreach (array_reverse($lines) as $line) {
print $line;
}
fclose($fp);
}
?>
</pre>

Leave a Reply