自定义函数-获取文件的前几行数据
/** * Method to tail (a few last rows) of a file. * * @param $filename * @param int $lines * @param int $buffer * * @return string */ public function tail($filename, $lines = 10, $buffer = 4096) { $f = fopen($filename, 'rb'); $output = ''; fseek($f, -1, SEEK_END); if ("\n" != fread($f, 1)) { --$lines; } while (ftell($f) > 0 && $lines >= 0) { $seek = min(ftell($f), $buffer); fseek($f, -$seek, SEEK_CUR); $output = ($chunk = fread($f, $seek)).$output; fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR); $lines -= substr_count($chunk, "\n"); } while ($lines++ < 0) { $output = substr($output, strpos($output, "\n") + 1); } fclose($f); return $output; }`