function getbigfile() {
$file_path = "D:/wamp/www/test/input.txt";
$handle = fopen($file_path, "r+");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 8000);
echo $buffer;
}
fclose($handle);
}
}
//getbigfile();
//die;
//返回文件从X行到Y行的内容
function getFileLine($file_path,$starline=1,$endline=5,$open_mode = "rb+"){
$fp = new SplFileObject($file_path, $open_mode);
$fp->seek($starline - 1); // 转到第N行, seek方法参数从0开始计数
for($i=0;$i<=$endline;$i++){
$content[] = $fp->current(); //current()获取当前行内容
$fp->next(); // 下一行
}
return $content;
}
$file_path = "D:/wamp/www/test/input.txt";
print_r(getFileLine($file_path,5,6));
PHP 读取大文件 SplFileObject
最新推荐文章于 2024-09-26 21:05:05 发布