php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26510 fgetcsv does not get all lines
Submitted: 2003-12-03 13:28 UTC Modified: 2003-12-06 17:04 UTC
From: thomas303 at web dot de Assigned: iliaa (profile)
Status: Closed Package: Filesystem function related
PHP Version: 4CVS-2003-12-4 OS: Suse Linux 8.2
Private report: No CVE-ID: None
 [2003-12-03 13:28 UTC] thomas303 at web dot de
Description:
------------
Hi,

I use the function fgetcsv, but it imported only 17860 of 20900 rows.

Tried \r\n and \n.

I found out, that rows are missing in between of the file, not at the beginning or the end. When I import only these rows (grepped before), everything works fine.

It imports some rows, misses one, imports some and so on.

The error occured the last days without a change in file structure or php. It could be the data itself which leads to the error. I can't reproduce it nor can I send you the file because of data protection reasons.

With fgets and explode everything is fine again. You should check against if fgetcsv works the same way or map fgetcsv to fgets and explode.

Reproduce code:
---------------
$fields=fgetcsv($fp,4096,"|");

with loop and so on.

Expected result:
----------------
$fields=fgetcsv($fp,4096,"|");

should work exactly the same as

$newline=fgets($fp,4096);
$fields=explode("|", $newline);




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-12-03 14:45 UTC] iliaa@php.net
Please provide the prolematic cvs file and shortest possible script that can be used to replicate the problem.
 [2003-12-03 15:03 UTC] thomas303 at web dot de
Hi,

I just wanted to send you some additional information about the way I tested:

I tested importing a File on 3 Systems:
- 2 times php4.3.3
- 1 time php4.3.5-dev

I used one file on all these tests and every time I got the problem with fgetcsv which got solved using gets and explode.

Using another system with php4.3.3, same data structure but other data in 30,000 rows, I have never had a problem. So it could be the data or maybe fgetcsv is not binary safe.

I didn't have the problem some days ago under same circumstances with nearly the same data and size. I spent hours in trying to reproduce why the error occurs, but I can't.

Maybe a look into the PHP source code helps you more than I can at the moment.

I can't give you the original file as I already wrote.

But I'll spend some minutes in creating a test script.

Best regards,
Thomas
 [2003-12-03 15:15 UTC] thomas303 at web dot de
#!/usr/local/bin/php
<?php

$file_name="pks.dat";

	$fp=fopen($file_name,"r");
	while(feof($fp)===False)
	{
		// The comment-lines should work instead of fgetcsv.
		// $new = fgets($fp,4096);
		// $new_field_values=explode("|",$new);
		$new_field_values=fgetcsv($fp,4096,"|");
		$zaehler++;
	}
	fclose ($fp);
	print $zaehler."\n";
?>
 [2003-12-03 15:19 UTC] thomas303 at web dot de
Hi,

using fget and explode (like written in the script above) $zaehler returns 20967.

Using fgetcsv it returns 17861 reading the same file.

I am really sorry, I can't provide the file.

Best regards
Thomas
 [2003-12-03 15:24 UTC] thomas303 at web dot de
Hi, me again...

in case I consider it correctly, fgetcsv is sometimes
- reading and processing a line, and twice jumping to the next line 
- or ignoring one line and processing the next one.

best regards
thomas
 [2003-12-03 15:37 UTC] iliaa@php.net
fgetcvs() can support multi-line rows, which may explain why a file with X number of lines when read with fgetcvs() would return less lines (but equal amount of total data).

If you cannot provide the original cvs file, could you provide an equivalent that can replicate the problem. Without a cvs file it's nearly impossible for us to resolve this problem.
 [2003-12-03 16:09 UTC] thomas303 at web dot de
Hi iliaa,

one thing, I did was to do a count on the array of fgetcsv.

In result it was alwas 18 values per line, so there is less data returned than data existing in the file, i think.

best regards
thomas
 [2003-12-04 20:15 UTC] iliaa@php.net
The reason you get unequal amount of lines is because you use an un-escaped delimiter ("), which causes multiple lines to be parsed a single value.

There is an unrelated bug in fgetcsv() in PHP 4.X, to which I am testing a patch now.
 [2003-12-06 17:04 UTC] iliaa@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 23:01:28 2024 UTC