php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12643 fgetcsv does some wrong things
Submitted: 2001-08-08 04:56 UTC Modified: 2002-06-18 17:49 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: Grant dot Walters at walters dot co dot nz Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 4.0.6 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: Grant dot Walters at walters dot co dot nz
New email:
PHP Version: OS:

 

 [2001-08-08 04:56 UTC] Grant dot Walters at walters dot co dot nz
<?

/*
- fgetcsv when used with non " delimiter causes problems
- script called from command line
- on a large file (25,000 lines) the script will core dump most times with a segmentation fault

Contents of gr
DOS (CR/LF) or Unix (LF) record termination makes no difference to result
------------------------------------------
26261~~5402211~yes~MASTERFULL~0
26263~~0126003045~yes~"PIONEERING"~0
26263~~039300358~yes~  "     CASSIOPEIA~0
26263~~91054745~yes~OLYMPIC~0
26261~~2302~yes~MASTERLESS~0
26263~~6003045~yes~PIONEERING~0
------------------------------------------
*/

$file_name = "/tmp/unix/gr";

// Faulty:
echo "\nFAULTY\n";
$text_file = fopen($file_name,"r");
if ($text_file) {
  $count=1;
  while ($data=fgetcsv($text_file,200,"~")) {
    echo "Record:$count:".$data[0].":".$data[2].":".$data[4]."\n";
    $count++;
  }
  echo "$file_name Complete\n";
}
fclose($text_file);

/*
Problem 1: This will stop in the third record because of the unmatched "
Problem 2: It strips the " characters out of the resultant array fields
*/

//Working:
echo "\nWORKING\n";
$text_file = fopen($file_name,"r");
if ($text_file) {
  $count=1;
  while ($data_in=fgets($text_file,200)) {
    $data=explode("~",$data_in);
    echo "Record:$count:".$data[0].":".$data[2].":".$data[4]."\n";
    $count++;
  }
  echo "$file_name Complete\n";
}
fclose($text_file);

/*
SCRIPT OUTPUT

FAULTY
Record:1:26261:5402211:MASTERFULL
Record:2:26263:0126003045:PIONEERING
/tmp/unix/gr Complete

WORKING
Record:1:26261:5402211:MASTERFULL
Record:2:26263:0126003045:"PIONEERING"
Record:3:26263:039300358:  "     CASSIOPEIA
Record:4:26263:91054745:OLYMPIC
Record:5:26261:2302:MASTERLESS
Record:6:26263:6003045:PIONEERING
/tmp/unix/gr Complete

*/

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-08-08 10:51 UTC] sniper@php.net
Reproduced with latest CVS of PHP.

--Jani

 [2002-06-18 06:34 UTC] derick@php.net
can you see if this is fixed in the latest non-stable cvs snapshot from snaps.php.net ?

Derick
 [2002-06-18 08:14 UTC] hholzgra@php.net
you mixed up delimiter and enclosure arguments here
and redefined only the delimiter from ',' to '~'

you also have to redefine the enclosure character 
to something you don't use in your data instead of
'"' to make your code work (4th parameter to fgetcsv())


 [2002-06-18 17:49 UTC] Grant dot Walters at walters dot co dot nz
So as my data that does not contain an "enclosure character", how should this be handled?  The current manual page states that the parameter cannot be null.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC