Grazie a tutti per il vostro aiuto. Ho usato una combinazione di risposte perché dovevo essere in grado di scegliere quali campi racchiudere e quali no. Ho aggiunto una stringa univoca (xxx) intorno ai campi che io non voglio racchiudere e utilizzando str_replace prima di scrivere la riga al file:
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='.$strFeederFileName.'');
function dumbcsv($file_handle, $data_array, $enclosure, $field_sep, $record_sep)
{
dumbescape(false, $enclosure);
$data_array=array_map('dumbescape',$data_array);
$line = $enclosure
. implode($enclosure . $field_sep . $enclosure, $data_array)
. $enclosure . $record_sep;
$line = str_replace('"xxx', '', $line);
$line = str_replace('xxx"', '', $line);
return $line;
}
function dumbescape($in, $enclosure=false)
{
static $enc;
if ($enclosure===false) {
return str_replace($enc, '\\' . $enc, $in);
}
$enc=$enclosure;
}
$output = fopen('php://output', 'r+');
$rows_csv = mysql_query('SELECT * FROM tblfeeder');
//Add file header
$fileheader = array("FH", "READY TO PAY", $fileheader, $strOracleBatchName);
$line = dumbcsv($output, $fileheader, "\"", ",", "\r\n");
fwrite($output, $line);
// loop over the rows, outputting them
while ($row_csv = mysql_fetch_assoc($rows_csv)) fwrite($output, dumbcsv($output, $row_csv, "\"", ",", "\r\n"));
//Add file footer
$filefooter = array("IF", "xxx".$batchtotal."xxx", "xxx".$invnum."xxx");
$line = dumbcsv($output, $filefooter, "\"", ",", "\r\n");
fwrite($output, $line);
tale opzione in php ... ma vedi http: // stackoverflow.com/questions/2514597/php-fputcsv-and-enclosing-fields per darti un'implementazione alternativa. – Orangepill
Non è necessario poiché non contiene caratteri come spazi bianchi, virgolette o caratteri di separazione. – Gumbo