php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #38496 fgetcsv() doesn't report delimiter/enclosure greater than 1 char like fputcsv.
Submitted: 2006-08-18 10:33 UTC Modified: 2006-08-18 12:51 UTC
From: RQuadling at GMail dot com Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 5CVS-2006-08-18 (snap) OS: Windows XP SP2
Private report: No CVE-ID: None
 [2006-08-18 10:33 UTC] RQuadling at GMail dot com
Description:
------------
Hi.

The issue was initially caught when I used '\t' rather than "\t".

The source for fputcsv reports notices when you use a delimiter or an enclosure of > 1 character. But fgetcsv doesn't report this.

The following patch should fix this.

Index: file.c
===================================================================
RCS file: /repository/php-src/ext/standard/file.c,v
retrieving revision 1.449
diff -u -r1.449 file.c
--- file.c      16 Jul 2006 15:54:25 -0000      1.449
+++ file.c      18 Aug 2006 10:30:51 -0000
@@ -2081,6 +2081,8 @@
                        if (delimiter_str_len < 1) {
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "delimiter must be a character");
                                RETURN_FALSE;
+                       } else if (delimiter_str_len > 1) {
+                               php_error_docref(NULL TSRMLS_CC, E_NOTICE, "delimiter must be a single character");
                        }

                        /* use first character from string */
@@ -2091,6 +2093,8 @@
                        if (enclosure_str_len < 1) {
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "enclosure must be a character");
                                RETURN_FALSE;
+                       } else if (enclosure_str_len > 1) {
+                               php_error_docref(NULL TSRMLS_CC, E_NOTICE, "enclosure must be a single character");
                        }
                        /* use first character from string */
                        enclosure = enclosure_str[0];



Reproduce code:
---------------
<?php
error_reporting(E_ALL);
++$dummy; // Proof of notices working.

// Store CSV like data in temporary file.
$fp = tmpfile();

// Real data I've been given - yeuch!
fwrite($fp,  <<< END_DATA
BO1`11519`112733`CELTIC002`2002/01/02````D.W.`Marilyn````````2`2``2`1225`10039
BO1`11520`=VARIOUS`HILL 1`2002/01/18````Various`Marilyn````````36``36`36`1225`VARIOUS
BO1`11521`+VARIOUS`HILL 1`2002/01/14````Various`Marilyn````````32``32`32`1225`VARIOUS
BO1`11522`\\VARIOUS`HILL 1`2002/01/14````Various`Marilyn````````133``133`133`1225`VARIOUS
BO1`11523`113027`FRAIKIN006`2002/01/02````S.W.`Marilyn```````10`1``1`1`1225`AAN124

END_DATA
);

// Reset the file pointer.
fseek($fp, 0, SEEK_SET);

// Get data and report the number of 
$a = array
	(
	fgetcsv($fp, 8192, '``'), // Expect notice - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "delimiter must be a single character")
	fgetcsv($fp, 8192, '`', '\t'), // Expect notice - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "enclosure must be a single character");
	fgetcsv($fp, 8192, '`', '\t'), // Expect notice - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "enclosure must be a single character");
	fgetcsv($fp, 8192, '`', '\t'), // Expect notice - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "enclosure must be a single character");
	fgetcsv($fp, 8192, '`', "\t"), // Nothing wrong with this one.
	);
foreach($a as $b)
	{
	// Each row has 23 values.
	echo count($b), ' ', $b[2], "\n";
	}
fclose($fp);
?>

Expected result:
----------------
Notices about enclosure or delimiter not being a single character like fputcsv

Actual result:
--------------
Notice: Undefined variable: dummy in C:\a.php on line 3
23 112733
23 =VARIOUS
23 +VARIOUS
3 VARIOUS`HILL 1`2002/01/14````Various`Marilyn````````133``133`133`1225`VARIOUS
BO1`11523`113027`FRAIKIN006`2002/01/02````S.W.`Marilyn```````10`1``1`1`1225`AAN124

1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-08-18 12:51 UTC] tony2001@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 04:01:30 2024 UTC