[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SplFileObject - Inconsistent behavior of seek and key with csv file #8121

Closed
Whip opened this issue Feb 20, 2022 · 0 comments
Closed

SplFileObject - Inconsistent behavior of seek and key with csv file #8121

Whip opened this issue Feb 20, 2022 · 0 comments

Comments

@Whip
Copy link
Whip commented Feb 20, 2022

Description

In the code below, I'm opening a csv file, seeking to each line and trying to get the contents. $file->current() returns the correct line everytime but $file->key() returns 0 and then 1 for each line. This only happens when with READ_AHEAD and READ_CSV flags are used.

Note that o() is just a wrapper for echo and print_r.

The following code:

<?php
$file = new SplFileObject('import2.csv', 'r');
$file->setFlags(SplFileObject::READ_AHEAD | SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);
$file->seek(0);
o($file->key());
o($file->current());
$file->seek(1);
o($file->key());
o($file->current());
$file->seek(2);
o($file->key());
o($file->current());
$file->seek(3);
o($file->key());
o($file->current());

Resulted in this output:

0
id,status,on_sale,brand,name,link,meta_title,meta_desc,description
1
1,2,15,Samsung,M21,samsung-m21,Samsung M21,Samsung M21,Samsung M21
1
2,2,15,Samsung,M32,samsung-m32,Samsung M32,Samsung M32,Samsung M32
1
3,2,15,Samsung,M21,samsung-m21,Samsung M21,Samsung M21,Samsung M21

But I expected this output instead:

0
id,status,on_sale,brand,name,link,meta_title,meta_desc,description
1
1,2,15,Samsung,M21,samsung-m21,Samsung M21,Samsung M21,Samsung M21
2
2,2,15,Samsung,M32,samsung-m32,Samsung M32,Samsung M32,Samsung M32
3
3,2,15,Samsung,M21,samsung-m21,Samsung M21,Samsung M21,Samsung M21

If I remove READ_CSV flag, the line numbers I get are

0
2
3
4

Finally, If I remove READ_AHEAD as well, I get the expected output.

Looping through

When not using seek, everything works fine.

$file = new SplFileObject('import2.csv', 'r');
$file->setFlags(SplFileObject::READ_AHEAD | SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);
foreach ($file as $row) {
	o($file->key());
}

Outputs

0
1
2
3

I am attaching the csv file I used as well.

import2.csv

PHP Version

PHP 8.1.2

Operating System

Windows 11, Xampp 3.3.0

cmb69 added a commit to cmb69/php-src that referenced this issue Feb 22, 2022
First, we must not free the current line before we call
`spl_filesystem_file_read_csv()`, because then the `current_line` will
not be properly updated.  Since the EOF check is superfluous here, we
move that part of the code to the branch for subtypes.  This issue has
been introduced by the fix for bug 75917.

Second, we only must increase the `current_line` if we're not reading
ahead.  This issue has been introduced by the fix for bug 62004.
@cmb69 cmb69 self-assigned this Feb 22, 2022
cmb69 added a commit to cmb69/php-src that referenced this issue Feb 24, 2022
First, we must not free the current line before we call
`spl_filesystem_file_read_csv()`, because then the `current_line` will
not be properly updated.  Since the EOF check is superfluous here, we
move that part of the code to the branch for subtypes.  This issue has
been introduced by the fix for bug 75917.

Second, we only must increase the `current_line` if we're not reading
ahead.  This issue has been introduced by the fix for bug 62004.
@cmb69 cmb69 closed this as completed in 1d9a1f9 Mar 8, 2022
cmb69 added a commit that referenced this issue Mar 8, 2022
* PHP-8.0:
  Fix GH-8121: SplFileObject - seek and key with csv file inconsistent
cmb69 added a commit that referenced this issue Mar 8, 2022
* PHP-8.1:
  Fix GH-8121: SplFileObject - seek and key with csv file inconsistent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
@cmb69 @Whip and others