$array_data = array();//Your data to put in CSV file.
$csvdata = ‘colum1,colum2,colum3’.”\n”;
for($p=0; $p<count($array_data);$p++){
$csvdata .= $array_data[$p]['field1'].','.$array_data[$p]['field2'].','.$array_data[$p]['field3']."\n";
}

$fname = 'filename.csv';
$fp = fopen($fname,'w');
fwrite($fp,$csvdata);
fclose($fp);

//To open file for download.
header('Content-type: application/csv');
header("Content-Disposition: inline; filename=".$fname);
header("Pragma: no-cache");
header("Expires: 0");
readfile($fname);

Advertisements