public static function readBySeek($filepath,$lines,$revers = false){
$offset = -1;
$c = '';
$read = '';
$i = 0;
$fp = fopen($filepath, "r");
while( $lines && fseek($fp, $offset, SEEK_END) >= 0 ) {
$c = fgetc($fp);
if($c == "\n" || $c == "\r"){
$lines--;
if( $revers ){
$read[$i] = strrev($read[$i]);
$i++;
}
}
if( $revers ) $read[$i] .= $c;
else $read .= $c;
$offset--;
}
fclose($fp);
if( $revers ){
if($read[$i] == "\n" || $read[$i] == "\r")
array_pop($read);
else $read[$i] = strrev($read[$i]); //反转字符串
return implode('',$read);
}
return strrev(rtrim($read,"\n\r"));
}
php fseek读取文件(从后往前读取)
最新推荐文章于 2023-04-06 16:52:19 发布