请注意,在 PHP 8.0.10 中,fseek 之后的 fgets 行为已更改,如果将 seek 设置为第 50 行并运行 fgets,它将返回第 50 行,而在 PHP 5.1~8.0.0 中,它将返回第 51 行。
<?php
$file = new SplTempFileObject();
for ($i = 0; $i < 100; $i++) {
$file->fwrite("Foo $i\n");
}
$file->seek(50);
echo json_encode(array(
array('line' => $file->key(), 'contents' => trim($file->fgets())),
array('line' => $file->key(), 'contents' => trim($file->fgets())),
array('line' => $file->key(), 'contents' => trim($file->fgets())),
), JSON_PRETTY_PRINT);
?>
结果
PHP 8.0.1+
[
{
"line": 50,
"contents": "Foo 50"
},
{
"line": 50,
"contents": "Foo 51"
},
{
"line": 51,
"contents": "Foo 52"
}
]
PHP 5.1 到 8.0.0
[
{
"line": 50,
"contents": "Foo 51"
},
{
"line": 51,
"contents": "Foo 52"
},
{
"line": 52,
"contents": "Foo 53"
}
]