#49708 [Bgs]: fgetcsv returns NULL instead of FALSE

From: Date: Tue, 29 Sep 2009 13:09:14 +0000
Subject: #49708 [Bgs]: fgetcsv returns NULL instead of FALSE
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-143037@lists.php.net to get a copy of this message
ID: 49708 User updated by: orel at melix dot net Reported By: orel at melix dot net Status: Bogus Bug Type: Filesystem function related Operating System: All PHP Version: 5.3SVN-2009-09-29 (SVN) New Comment: So, documentation of return value needs update. Previous Comments: ------------------------------------------------------------------------ [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 http://bugs.php.net/?id=49708&edit=1

« previous php.bugs (#143037) next »