php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45788 fgetcsv retuns NULL on warning instead of false
Submitted: 2008-08-11 18:36 UTC Modified: 2008-08-12 16:11 UTC
From: steven at realestatewebmasters dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.2.6 OS: Linux
Private report: No CVE-ID: None
 [2008-08-11 18:36 UTC] steven at realestatewebmasters dot com
Description:
------------
fgetcsv returns null if passed false as the handle. This should cause and error instead of a warning and cause the function to return false.

Reproduce code:
---------------
<?php

$handle = fopen("foo","r");

var_dump($data = fgetcsv($handle,0,",",'"'));

while (($data = fgetcsv($handle,0,",",'"')) !== false) {
	echo 'test';
}

?>

Expected result:
----------------
Warning: fopen(foo): failed to open stream: No such file or directory in /home/sbarre85/test.php on line 3

Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /home/sbarre85/test.php on line 5
bool(false)

Error: fgetcsv() expects parameter 1 to be resource, boolean given in /home/sbarre85/test.php on line 7

Actual result:
--------------
Warning: fopen(foo): failed to open stream: No such file or directory in /home/sbarre85/test.php on line 3

Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /home/sbarre85/test.php on line 5
NULL

Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /home/sbarre85/test.php on line 7
test

.. looping infinitly

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-12 14:24 UTC] jani@php.net
Invalid parameter does not cause fatal error in any PHP function. And when an invalid parameter is passed, all functions using the new (more consistent) parameter parsing also return NULL. No bug here.
 [2008-08-12 16:11 UTC] steven at realestatewebmasters dot com
I accept that it should not throw an error.

I do think it should act like all the other file functions (like fgetc() ) that will return false on invalid parameters.

At the very least could the documentation for this function be updated? The current example:

$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

will cause an infinite loop if you fail to open the file.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 20:01:29 2024 UTC