From:             thuejk at gmail dot com
Operating system: Ubuntu
PHP version:      5.3.1
PHP Bug Type:     Unknown/Other Function
Bug description:  fputcsv and fgetcsv doesn't follow CSV format

Description:
------------
According to http://en.wikipedia.org/wiki/Comma-separated_values (which I
assume is quoting RFC 4180), CSV does not have an escape character, but
instead quotes fields with ", and escapes '"' with '""'.

But fputcsv() escapes '"' with '\"', and fgetcsv() incorrectly chokes on
the fragment '\""' (which should be unescaped to '\"').

This is a problem for for example StarOffice, which actually implements
the standard, and (understandably) chokes on fputcsv() output like
"\"",2,3

I haven't tested if this is also a problem in MS office, but it is if MS
Office implements the CSV standard correctly.

Note that the fputcsv() manual at
http://dk.php.net/manual/en/function.fputcsv.php says: "Format line as
CSV", and similarly with fgetcsv(), so there is no doubt that the PHP
functions should follow the RFC.

This bug seems to be the result of bug report
http://bugs.php.net/bug.php?id=22382 , which is obviously bogus, but was
"fixed" by breaking php's CSV support.

Reproduce code:
---------------
<?php
echo "<pre>fputcsv:\n";

$matrix = Array(Array('a\\"b',2,3));
$temp_handle = tmpfile();
foreach ($matrix as $row) {
  fputcsv($temp_handle, $row);
}
fseek($temp_handle, 0) === 0;
$str = fread($temp_handle, 1024*1024*100);
echo $str;
fclose($temp_handle); // this removes the tmpfile() file

echo "\nfgetcsv:\n";
$temp_handle = tmpfile();
fwrite($temp_handle, '"a\\""", 2, 3');
fseek($temp_handle, 0);
$a = fgetcsv($temp_handle);
echo $a[0];
fclose($temp_handle); // this removes the tmpfile() file
?>


Expected result:
----------------
fputcsv:
"a\""b",2,3

fgetcsv:
a\"


Actual result:
--------------
fputcsv:
"a\"b",2,3

fgetcsv:


-- 
Edit bug report at http://bugs.php.net/?id=50686&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=50686&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=50686&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=50686&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=50686&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=50686&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=50686&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=50686&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=50686&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=50686&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=50686&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=50686&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=50686&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=50686&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=50686&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=50686&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=50686&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=50686&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=50686&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=50686&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=50686&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=50686&r=mysqlcfg

Reply via email to