note 126365 added to splfileobject.seek

From: Date: Thu, 26 Aug 2021 12:02:56 +0000
Subject: note 126365 added to splfileobject.seek
Groups: php.notes 
Request: Send a blank email to php-notes+get-216826@lists.php.net to get a copy of this message
there is a bug using fseek on previos versions of PHP8.0.1 See: https://bugs.php.net/bug.php?id=46569 https://3v4l.org/O89dJ I solved it with a function: <?php /** * SEEK an Spl object * There is a bug in php for seeking files * seems solved php_version > PHP8.0.1 * See https://bugs.php.net/bug.php?id=46569 * & https://3v4l.org/O89dJ * * $Spl->seek() Works ok in all versions with offset 0 (first row) * On PHP_VERSION < 8.0.1: * - Offset 1: seek() cannot seek at row 1. It will be done manually, rewind file and reading first row * - Rest of Offsets: The cursor remains at next row of $Offset * * @param SplObject $Spl * @param int $Offset */ function seek_spl($Spl, $Offset){ if (version_compare(PHP_VERSION, '8.0.1', '>=') || $Offset == 0) { $Spl->seek($Offset); } else { if( $Offset == 1 ){ $Spl->rewind(); // Ensure to go at first row before exit $Spl->fgets(); // Read line 0. Cursor remains now at line 1 } else { $Spl->seek($Offset-1); } } }?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 81.43.64.136) ---- Manual Page -- https://php.net/manual/en/splfileobject.seek.php Edit -- https://main.php.net/note/edit/126365 Del: integrated -- https://main.php.net/note/delete/126365/integrated Del: useless -- https://main.php.net/note/delete/126365/useless Del: bad code -- https://main.php.net/note/delete/126365/bad+code Del: spam -- https://main.php.net/note/delete/126365/spam Del: non-english -- https://main.php.net/note/delete/126365/non-english Del: in docs -- https://main.php.net/note/delete/126365/in+docs Del: other reasons-- https://main.php.net/note/delete/126365 Reject -- https://main.php.net/note/reject/126365 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#216826) next »