|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28869 possible wrong behavior of fgetcsv (?)
Submitted: 2004-06-21 16:58 UTC Modified: 2004-06-22 00:44 UTC
From: laph at gmx dot net Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 4.3.6 OS: All
Private report: No CVE-ID: None
 [2004-06-21 16:58 UTC] laph at gmx dot net
Assume a file with 1 line of text (toImport.csv) and a [newline] after the last char in that line.
The code below will echo "2" (Lines) using either fgets or fgetcsv.
While this is right for fgets, it's wrong for fgetcsv (I think), because in CSV-Files the [newline] (logically) terminates a row, it doesn't start a new one - at least if you think of a CSV-File as a Recordset from a SQL-Statement (If you read the last "line" of a SQL-Result, move the Row-Pointer to the next row and try to read it, you'll get false/EOF and not a row with 0 elements).
If you return the Array "$data" in each iteration you get a array with (int) 0 elements as last row, what makes using a loop until EOF impossible, because you (additionally) have to check the number of elements returned in $data.

It's quite anoying, that fgetcsv makes difference if a csv-file is terminated by a newline or not.

Reproduce code:
$fh = fopen(dirname(__FILE__)."/toImport.csv","r");

$row = 0;
while (!feof($fh)) {
	$data = fgetcsv($fh,1000,";");
	// $data = fgets($fh,1000);

Expected result:
fgetcsv should echo 1, while fgets should echo 2


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-22 00:44 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

This is expected. 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Jan 16 15:01:23 2021 UTC