Forum Moderators: coopster
$row = 1;
$handle = fopen($uploadFile, "r"); while ( ($data = fgetcsv($handle, 10000, ","))!= FALSE ) {
$num = count($data);
if($row == 1){
$fields = " `id` , ";
for($i=0; $i<$num; $i++){
$fields .= "`" . addslashes(trim($data[$i])) . "` , ";
}
$fields .= "`group` , `user` , ";
$f = substr($fields, 0, -1);
$f = substr($f, 0, -1);
$row++;
}else{
$values = " '' , ";
for($i=0; $i<$num; $i++){
$values .= "'" . addslashes(trim($data[$i])) . "' , ";
}
$values .= "'personal' , '" . $_SESSION['user'] . "' , ";
$v = substr($values, 0, -1);
$v = substr($v, 0, -1);
$sql = "INSERT INTO `addressbook` ( $f ) VALUES ( $v )";
mysql_query($sql) or die(print "<center><font size='2' face='verdana' color='#5E6B79'><b>Upload complete some entries may not have been loaded successfully!</b><br><br><a href='addressbook.php' class='light'>Back to Address Book</a></font></center></td></tr></table>");
$values = '';
$row++;
}
}
fclose ($handle);
can anyone help? thanx
There is a comment on the manual page for fgetcsv that recommends trying the File_CSV [pear.php.net]PEAR class - so you might want to give that a go.
Strictly speaking, any quote character that apears within a quote qualified string should be escaped; but the real problem is that there are simply no formal specifications of how a CSV file should handle such anomolies.
This is a common problem in the world of affiliate datafeeds - many are created with little care and attention; the contents of a database are simply written out to a text file without thinking about it. The same problem occurs when what you could loosly describe as an "XML" feed contains HTML markup that has just been written into the feed willy nilly!
Alternatively, is it possible to use a different delimiter on export that is not in your actual data? For example, you could use a pipe, then you would just do a search and replace to change all " to \" Then you would replace all pipes with quotes and be good to go.