Edit report at https://bugs.php.net/bug.php?id=49708&edit=1
ID: 49708
Comment by: jameshfisher at gmail dot com
Reported by: orel at melix dot net
Summary: fgetcsv returns NULL instead of FALSE
Status: Not a bug
Type: Bug
Package: Filesystem function related
Operating System: All
PHP Version: 5.3SVN-2009-09-29 (SVN)
Block user comment: N
Private report: N
New Comment:
jani@php.net is clearly wrong here. The documentation specifies that this method "Returns an
indexed array containing the fields read, or FALSE on error." As Orel as shown, the
implementation does not satisfy this specification. This is the definition of a bug; therefore this
is a bug.
There are two possible fixes here: fix the implementation, or fix the specification. Doing neither
is not an option.
Therefore, this bug must be re-opened.
Previous Comments:
------------------------------------------------------------------------
[2009-09-29 13:09:11] orel at melix dot net
So, documentation of return value needs update.
------------------------------------------------------------------------
[2009-09-29 12:41:19] jani@php.net
Invalid parameters cause always (almost) in any function NULL to be returned. This is not a bug.
------------------------------------------------------------------------
[2009-09-29 12:00:16] orel at melix dot net
I agree with steven, doc and example should be updated or function should return FALSE (like fclose,
fgets, fwrite ...)
Patch for version 6
Index: ext/standard/file.c
===================================================================
--- ext/standard/file.c (revision 288932)
+++ ext/standard/file.c (working copy)
@@ -2166,7 +2166,7 @@
&delimiter, &delimiter_len, &delimiter_type,
&enclosure, &enclosure_len, &enclosure_type,
&escape, &escape_len, &escape_type) == FAILURE) {
- return;
+ RETURN_FALSE;
}
PHP_STREAM_TO_ZVAL(stream, &zstream);
Patch for 5.3
Index: ext/standard/file.c
===================================================================
--- ext/standard/file.c (revision 288932)
+++ ext/standard/file.c (working copy)
@@ -2034,7 +2034,7 @@
&enclosure_str, &enclosure_str_len,
&escape_str, &escape_str_len) == FAILURE
) {
- return;
+ RETURN_FALSE;
}
if (delimiter_str != NULL) {
------------------------------------------------------------------------
[2009-09-29 11:59:52] orel at melix dot net
Description:
------------
See example of documentation http://www.php.net/fgetcsv
If file doesn't exist there is an endless loop.
fgetcsv returns NULL if handler is invalid.
Reproduce code:
---------------
<?php
$row = 1;
$handle = fopen("test.csv", "r"); /* test.csv should not exist */
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
?>
Expected result:
----------------
fgetcsv should returns FALSE
Actual result:
--------------
Endless loop, fgetcsv returns NULL
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=49708&edit=1