php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66001 Problem with SplFileObject::seek()
Submitted: 2013-10-30 12:10 UTC Modified: 2020-11-18 13:14 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (66.7%)
From: miichal dot kozlowski at gmail dot com Assigned: cmb (profile)
Status: Duplicate Package: SPL related
PHP Version: Irrelevant OS: Win & Unix
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: miichal dot kozlowski at gmail dot com
New email:
PHP Version: OS:

 

 [2013-10-30 12:10 UTC] miichal dot kozlowski at gmail dot com
Description:
------------
Hi,
Example 1 shows we set $file->seek(0) and it will return 3 rows:

Array
(
    [0] => 0DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => test
    [4] => FL
    [5] => 34221
)
Array
(
    [0] => 1DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => Test
    [4] => FL
    [5] => 34221
)
Array
(
    [0] => 2DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => Test
    [4] => FL
    [5] => 34221
)

Example 2 shows if we set $file->seek(1) then we are expecting to get 2 rows:

Array
(
    [0] => 1DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => Test
    [4] => FL
    [5] => 34221
)
Array
(
    [0] => 2DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => Test
    [4] => FL
    [5] => 34221
)

 ,but we received 1.

Array
(
    [0] => 2DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => Test
    [4] => FL
    [5] => 34221
)



Test script:
---------------
<?php
file_put_contents('file.csv', '"0DATA";"1 St 1 Av";"";"test";"FL";"34221"' . PHP_EOL .' "1DATA";"1 St 1 Av";"";"Test";"FL";"34221"' . PHP_EOL .' "2DATA";"1 St 1 Av";"";"Test";"FL";"34221"');
//example 1
$file = new SplFileObject('file.csv');
$file->seek(0);
while(!$file->eof()) {
    print_r($file->fgetcsv(";"));
}
echo '---' . PHP_EOL;
//example 2
$file = new SplFileObject('file.csv');
$file->seek(1);
while(!$file->eof()) {
    print_r($file->fgetcsv(";"));
}

Expected result:
----------------
Array
(
    [0] => 0DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => test
    [4] => FL
    [5] => 34221
)
Array
(
    [0] => 1DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => Test
    [4] => FL
    [5] => 34221
)
Array
(
    [0] => 2DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => Test
    [4] => FL
    [5] => 34221
)
example 2
Array
(
    [0] => 1DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => Test
    [4] => FL
    [5] => 34221
)
Array
(
    [0] => 2DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => Test
    [4] => FL
    [5] => 34221
)

Actual result:
--------------
Array
(
    [0] => 0DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => test
    [4] => FL
    [5] => 34221
)
Array
(
    [0] => 1DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => Test
    [4] => FL
    [5] => 34221
)
Array
(
    [0] => 2DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => Test
    [4] => FL
    [5] => 34221
)
example 2
Array
(
    [0] => 2DATA
    [1] => 1 St 1 Av
    [2] => 
    [3] => Test
    [4] => FL
    [5] => 34221
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-29 17:54 UTC] cmb@php.net
After calling SplFileObject::seek() the following line is already
read, and available via SplFileObject::current(), see example
#1[1].

However, given that your first example works, the behavior appears
to be indeed inconsistent. By the way, the issue is not
particularly related to ::fgetcsv(); fgets() shows the same
behavior.

[1] <http://php.net/manual/en/splfileobject.seek.php#refsect1-splfileobject.seek-examples>
 [2016-06-30 12:29 UTC] miichal dot kozlowski at gmail dot com
Yes you are right,
the problem is when you are working on a file and you wish to rerun the program starting from the first line. In such case you are not able to use SplFileObject::current().

That was my case, I wrote a pice of script which needed to rerun the same file from the place which you marked.
 [2019-05-06 06:56 UTC] maxlogic dot ga at gmail dot com
Short description: 

seek(0) then fgetcsv() - reading from 1st line
seek(1) then fgetcsv() - reading from 3rd line
seek(2) then fgetcsv() - reading from 4th line

So no way to reach 2nd line using seek.

I need to implement some "elegant" solution to avoid this bug.
 [2020-01-15 16:24 UTC] wowlinuxdebian at gmail dot com
Same here, as maxlogic: 

seek(0) then fgetcsv() - reading from 1st line
seek(1) then fgetcsv() - reading from 3rd line

how I can seek to line 2?
 [2020-11-18 13:14 UTC] cmb@php.net
-Status: Open +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2020-11-18 13:14 UTC] cmb@php.net
Well, actually this is a duplicate of bug #62004.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 06:01:29 2024 UTC