$r = readBackward('mydata.csv', 1); print_r($r);
function readBackward($file, $length = 0 ){ $buffer = 8 * 1024; $nl = "\n"; $lines = array(); $last = ''; $fh = fopen($file, 'r');
if(!$fh) { die('Fehler beim öffnen von:' . $file . ' => ' . $php_errormsg);}
fseek($fh, 0, SEEK_END); $pos = ftell($fh);
while($pos > 1){ $pos -= $buffer;
if($pos < 0) { $buffer += $pos; $pos = 0;
} fseek($fh, $pos, SEEK_SET); $tmp = explode($nl, fread($fh, $buffer) . $last);
if($pos > 0) {$last = array_shift($tmp);} $lines = array_merge($tmp, $lines);
if($length && count($lines) >= $length) { break; }
}
if($length && count($lines) >= $length) { $lines = array_slice($lines, -$length);
}
return $lines;
}