I forgot to mention in my previous note about PHP PHP 8.0.10, that you can use $file->current(); $file->next(); as a replacement for $file->fgets(); that works consistently from PHP 5.1 to 8.0.1+ after a seek():
<?php
$file = new SplTempFileObject();
for ($i = 0; $i < 100; $i++) {
$file->fwrite("Foo $i\n");
}
$file->seek(50);
print_r(array(
array('line' => $file->key(), 'contents' => trim($file->current()), 'triggerNext' => $file->next()),
array('line' => $file->key(), 'contents' => trim($file->current()), 'triggerNext' => $file->next()),
array('line' => $file->key(), 'contents' => trim($file->current()), 'triggerNext' => $file->next()),
));
?>
PHP 5.1 to 8.0.1+:
[
{
"line": 50,
"contents": "Foo 50"
},
{
"line": 51,
"contents": "Foo 51"
},
{
"line": 52,
"contents": "Foo 52"
}
]