src/LogViewer.php
public function fetch($seek = 0, $lines = 20, $buffer = 4096)
    {
        $f = fopen($this->filePath, 'rb');

        if ($seek) {
            fseek($f, abs($seek));
        } else {
            fseek($f, 0, SEEK_END);
        }

        if (fread($f, 1) != "\n") {
            $lines -= 1;
        }
        fseek($f, -1, SEEK_CUR);

        // 从前往后读,上一页
        // Start reading
        if ($seek > 0) {
            $output = '';

            $this->pageOffset['start'] = ftell($f);

            while (!feof($f) && $lines >= 0) {
                $output = $output.($chunk = fread($f, $buffer));
                $lines -= substr_count($chunk, "\n[20");
            }

            $this->pageOffset['end'] = ftell($f);

            while ($lines++ < 0) {
                $strpos = strrpos($output, "\n[20") + 1;
                $_ = mb_strlen($output, '8bit') - $strpos;
                $output = substr($output, 0, $strpos);
                $this->pageOffset['end'] -= $_;
            }

            // 从后往前读,下一页
        } else {
            $output = '';

            $this->pageOffset['end'] = ftell($f);

            while (ftell($f) > 0 && $lines >= 0) {
                $offset = min(ftell($f), $buffer);
                fseek($f, -$offset, SEEK_CUR);
                $output = ($chunk = fread($f, $offset)).$output;
                fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
                $lines -= substr_count($chunk, "\n[20");
            }

            $this->pageOffset['start'] = ftell($f);

            while ($lines++ < 0) {
                $strpos = strpos($output, "\n[20") + 1;
                $output = substr($output, $strpos);
                $this->pageOffset['start'] += $strpos;
            }
        }

        fclose($f);

        return $this->parseLog($output);
    }
public function fetch($seek = 0, $lines = 20, $buffer = 4096)
    {
        $f = fopen($this->filePath, 'rb');

        if ($seek) {
            fseek($f, abs($seek));
        } else {
            fseek($f, 0, SEEK_END);
        }

        if (fread($f, 1) != "\n") {
            $lines -= 1;
        }
        fseek($f, -1, SEEK_CUR);

        // 从前往后读,上一页
        // Start reading
        if ($seek > 0) {
            $output = '';

            $this->pageOffset['start'] = ftell($f);

            while (!feof($f) && $lines >= 0) {
                $output = $output.($chunk = fread($f, $buffer));
                $lines -= substr_count($chunk, "\n[20");
            }

            $this->pageOffset['end'] = ftell($f);

            while ($lines++ < 0) {
                $strpos = strrpos($output, "\n[20") + 1;
                $_ = mb_strlen($output, '8bit') - $strpos;
                $output = substr($output, 0, $strpos);
                $this->pageOffset['end'] -= $_;
            }

            // 从后往前读,下一页
        } else {
            $output = '';

            $this->pageOffset['end'] = ftell($f);

            while (ftell($f) > 0 && $lines >= 0) {
                $offset = min(ftell($f), $buffer);
                fseek($f, -$offset, SEEK_CUR);
                $output = ($chunk = fread($f, $offset)).$output;
                fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
                $lines -= substr_count($chunk, "\n[20");
            }

            $this->pageOffset['start'] = ftell($f);

            while ($lines++ < 0) {
                $strpos = strpos($output, "\n[20") + 1;
                $output = substr($output, $strpos);
                $this->pageOffset['start'] += $strpos;
            }
        }

        fclose($f);

        return $this->parseLog($output);
    }
public function fetch ( $ seek = 0 , $ lines = 20 , $ buffer = 4096 ) { $ f = fopen ( $ this -> filePath , 'rb' ) ; if ( $ seek ) { fseek ( $ f , abs ( $ seek ) ) ; } else { fseek ( $ f , 0 , SEEK_END ) ; } if ( fread ( $ f , 1 ) != "\n" ) { $ lines -= 1 ; } fseek ( $ f , - 1 , SEEK_CUR ) ; // 从前往后读,上一页 // Start reading if ( $ seek > 0 ) { $ output = '' ; $ this -> pageOffset [ 'start' ] = ftell ( $ f ) ; while ( ! feof ( $ f ) && $ lines >= 0 ) { $ output = $ output . ( $ chunk = fread ( $ f , $ buffer ) ) ; $ lines -= substr_count ( $ chunk , "\n[20" ) ; } $ this -> pageOffset [ 'end' ] = ftell ( $ f ) ; while ( $ lines ++ < 0 ) { $ strpos = strrpos ( $ output , "\n[20" ) + 1 ; $ _ = mb_strlen ( $ output , '8bit' ) - $ strpos ; $ output = substr ( $ output , 0 , $ strpos ) ; $ this -> pageOffset [ 'end' ] -= $ _ ; } // 从后往前读,下一页 } else { $ output = '' ; $ this -> pageOffset [ 'end' ] = ftell ( $ f ) ; while ( ftell ( $ f ) > 0 && $ lines >= 0 ) { $ offset = min ( ftell ( $ f ) , $ buffer ) ; fseek ( $ f , - $ offset , SEEK_CUR ) ; $ output = ( $ chunk = fread ( $ f , $ offset ) ) . $ output ; fseek ( $ f , - mb_strlen ( $ chunk , '8bit' ) , SEEK_CUR ) ; $ lines -= substr_count ( $ chunk , "\n[20" ) ; } $ this -> pageOffset [ 'start' ] = ftell ( $ f ) ; while ( $ lines ++ < 0 ) { $ strpos = strpos ( $ output , "\n[20" ) + 1 ; $ output = substr ( $ output , $ strpos ) ; $ this -> pageOffset [ 'start' ] += $ strpos ; } } fclose ( $ f ) ; return $ this -> parseLog ( $ output ) ; }
Fetch logs by giving offset.

@param int $seek
@param int $lines
@param int $buffer

@return array

@see http://www.geekality.net/2011/05/28/php-tail-tackling-large-files/
Fetch logs by giving offset .