I've got a script that allows the client to import a csv file into a database table. Everything works great - unless they have a field with a comma in it. In this case the data, instead of having 20,000 in the proper field, has "20 in one field and 000" bumped into the next field. How do I get that data to stay together when my delimeter is another comma, but not print the quotation marks?
<?
$linecount = 0;
$cust_id = "\0";
// If there were no upload errors, then continue
if (!$FILES['infile']['error']) {
$filename = $FILES['infile']['tmp_name'];
}
// Make sure we're getting a csv file
if (!check_type($_FILES['infile']['name'])) { die("Filetype must be CSV.<br>"); }
// Open our new file from the temporary upload folder, or quit
// if something's wrong
$file = fopen($filename, 'r') or die("Can't open $filename.");
// Looping until end of file
while (!feof($file)) {
// These files use UNIX style newlines, so normal fgets() won't parse them right.
// I'm using this custom function instead
$buffer = getline($file, "\r");
$linecount++;
// Escape ' and '' so as to not threaten the INSERT statement's fields
$buffer = str_replace("'", "\'", $buffer);
$buffer = str_replace('"', "\"", $buffer);
// Data is comma delimited, so explode it into an array
$field = split(',', $buffer);
// Insert record, only if the jobid field is not blank
if (strlen($field[0]) > 0) { insert_record($field, $cust_id); }
}
fclose($file);
///////////////////////////////////////////////////////////
// Begin function definitions
///////////////////////////////////////////////////////////
// I got this function from comments on fgets() on php.net
function getline( $fp, $delim )
{
$result = "";
while( !feof( $fp ) )
{
$tmp = fgetc( $fp );
if( $tmp == $delim )
return $result;
$result .= $tmp;
}
return $result;
}
// Quick check to make sure the file extension is "csv"
function check_type ($filename) {
$parts = split('.', $filename);
return (strtolower($parts[count($parts) - 1]) == 'csv')?true:false;
}
// Inserts one record into the 'reports' table
function insert_record($field, $customer_id = "\0") {
// Server information taken directly from your table description
// (Connection Info is here )
// Assuming all the fields in the CSV file are in the same order as
// the MySQL database, this should insert them all.
mysql_query("INSERT INTO
reports
VALUES
('$field[0]',
'$field[1]',
'$field[2]',
'$field[3]',
'$field[4]',
'$field[5]',
'$field[6]',
'$field[7]',
'$field[8]',
'$field[9]',
'$field[10]',
'$field[11]',
'$field[12]',
'$field[13]',
'$field[14]',
'$field[15]',
'$field[16]',
'$field[17]',
'$field[18]',
'$field[19]',
'$field[20]',
'$field[21]',
'$field[22]',
'$field[23]',
'$field[24]',
'$field[25]',
'$field[26]',
'$field[27]',
'$field[28]',
'$field[29]',
'$field[30]',
'$field[31]',
'$field[32]',
'$field[33]',
'$field[34]',
'$field[35]',
'$field[36]',
'$customer_id',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'HPC',
'Total Coliforms',
'Fecal Coliforms',
'TPC',
'Coliform Count',
'E. coli Count',
'S. aureus Count',
'Mold Count',
'Yeast Count',
'B. cereus Count',
'Salmonella',
'Listeria',
'E. coli',
'Campylobacter',
'% Protein',
'% Fat',
'% Moisture',
'% Ash',
'Peroxide Value',
'% Sodium',
'% Salt',
'PCB/CHC',
'Aflatoxin',
'T2',
'Vomitoxin',
'Sacox',
'Flash Point',
'Total Organic Carbon',
'pH',
'')", $connection);
}
?>