感谢你的帮助。我使用了多个答案的组合,因为我需要能够选择要包含哪些字段以及不包含哪些字段。在将行写入文件之前,我在不想包含的字段周围添加了一个唯一字符串 (xxx) 并使用 str_replace:
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);