note 49321 deleted from function.fgetcsv by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 18:18:14 +0000 |
| Subject: | note 49321 deleted from function.fgetcsv by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155097@lists.php.net to get a copy of this message | ||
Note Submitter: ramdac at ramdac dot org
----
Be wary of Example #1 above.
If the file doesn't exist, the application will spit out data to STDOUT and could fill up /tmp
space if you're not careful. The output might will look like this:
PHP Warning: fgetcsv(): supplied argument is not a valid stream resource in...(your filename here)
To get around this, do the following:
<?
row = 1;
if(!$handle = fopen("test.csv", "r"))
{
print 'could not open file. quitting';
die;
}
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);
?>