php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50456 fgetcsv is not binary-safe in different locales
Submitted: 2009-12-11 22:57 UTC Modified: 2009-12-12 01:31 UTC
From: pahan at hubbitus dot spb dot su Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.3.1 OS: Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: pahan at hubbitus dot spb dot su
New email:
PHP Version: OS:

 

 [2009-12-11 22:57 UTC] pahan at hubbitus dot spb dot su
Description:
------------
If in unicode locale we construct CSV-file by fputcsv some values, such as single words does not enclosed (even if enclosing character was directly provided). Because that, in different locale file can't be parsed properly. Be aware, I seen note in documentation, but I speak about binary-safe parsing, do not interpret any symbols.

In followed example we get result csv-string:
Test;????;"??? ?????"
Single wod is not enclosed. And I do not seen parameter to force enclosing it. But, it can completely solve problem! So, csv-string:
"Test";"????";"??? ?????"
correctly parsed in any locale (I repeat, I do not speak about further symbol interpretation)!

So, as easy fix, which also should not produce any backward capability problem my suggestion is add parameter to fputcsv to force enclosing fields, even if it consist from 1 word. Or, may be even do this as default behavior.

Reproduce code:
---------------
<?
$fp = fopen('php://temp', 'w');
fputcsv($fp, array('Test', '????', '??? ?????'), ';', '"');
rewind($fp);
echo(stream_get_contents($fp));
rewind($fp);
var_dump(fgetcsv($fp, 0, ';', '"'));
setlocale(LC_ALL, 'C');
rewind($fp);
var_dump(fgetcsv($fp, 0, ';', '"'));
?>

Expected result:
----------------
Test;????;"??? ?????"
array(3) {
  [0]=>
  string(4) "Test"
  [1]=>
  string(8) "????"
  [2]=>
  string(17) "??? ?????"
}
array(3) {
  [0]=>
  string(4) "Test"
  [1]=>
  string(0) "????"
  [2]=>
  string(17) "??? ?????"
}


Actual result:
--------------
Test;????;"??? ?????"
array(3) {
  [0]=>
  string(4) "Test"
  [1]=>
  string(8) "????"
  [2]=>
  string(17) "??? ?????"
}
array(3) {
  [0]=>
  string(4) "Test"
  [1]=>
  string(0) ""
  [2]=>
  string(17) "??? ?????"
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-12 01:30 UTC] jani@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.

See bug #45708
 [2009-12-12 01:31 UTC] jani@php.net
Sorry, meant bug #48507
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 24 01:01:29 2024 UTC