php.net |  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
Description:
------------
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);
	$row++;
}
echo($row);

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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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

This is expected. 
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 22:01:31 2024 UTC