<?php
$txt = <<<TXT
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
TXT;
class FileObject extends SplFileObject
{
#[\ReturnTypeWillChange]
public function seek($offset)
{
if ($offset < 0) {
throw new Exception("Can't seek file: " . $this->getPathname() . " to negative offset: $offset");
}
if ($offset === 0) {
parent::seek(0);
return;
}
$this->rewind();
for ($i = 0; $i < $offset; $i++) {
$this->fgets();
$this->next();
if ($this->eof()) {
break;
}
}
//$this->current();
}
}
$file = new FileObject('php://memory', 'w+');
$file->fwrite($txt);
echo 'Seeking line 2...', PHP_EOL;
$file->seek(1);
echo 'Position in the file: ', $file->ftell(), PHP_EOL;
echo 'Rest of the file: ', PHP_EOL, $file->fread(5000);
echo PHP_EOL, '-------------', PHP_EOL;
echo 'Seeking line last line...', PHP_EOL;
$file->seek(PHP_INT_MAX);
$lastLine = $file->key();
$offset = max($lastLine - 1, 0);
$file->seek($offset);
echo 'Position in the file: ', $file->ftell(), PHP_EOL;
echo 'Rest of the file: ', PHP_EOL, $file->fread(5000);
Seeking line 2...
Position in the file: 7
Rest of the file:
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
-------------
Seeking line last line...
Position in the file: 55
Rest of the file:
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Seeking line 2...
Position in the file: 7
Rest of the file:
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
-------------
Seeking line last line...
Position in the file: 55
Rest of the file:
Seeking line 2...
Position in the file: 7
Rest of the file:
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
-------------
Seeking line last line...
Position in the file: 49
Rest of the file:
Line 8
Seeking line 2...
Position in the file: 7
Rest of the file:
Fatal error: Call to undefined method FileObject::fread() in /in/Uhlpb on line 47
Process exited with code 255.
Output for 5.1.0 - 5.1.1, 5.1.3
Fatal error: Uncaught exception 'RuntimeException' with message 'SplFileObject::__construct(php://memory): failed to open stream: Success' in /in/Uhlpb:41
Stack trace:
#0 /in/Uhlpb(41): SplFileObject->__construct('php://memory', 'w+')
#1 {main}
thrown in /in/Uhlpb on line 41
Process exited with code 255.
Output for 5.1.2
Fatal error: Uncaught exception 'RuntimeException' with message 'SplFileObject::__construct(php://memory): failed to open stream: Success' in /in/Uhlpb:42
Stack trace:
#0 /in/Uhlpb(42): SplFileObject->__construct('php://memory', 'w+')
#1 {main}
thrown in /in/Uhlpb on line 42
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Fatal error: Class 'SplFileObject' not found in /in/Uhlpb on line 14
Process exited with code 255.
Output for 4.4.3 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Uhlpb on line 17
Process exited with code 255.
Output for 4.4.2
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Uhlpb on line 18
Process exited with code 255.
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Uhlpb on line 17
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/Uhlpb on line 17
Process exited with code 255.