Add heading on export using fputcsv php

This worked for me.

function downloadCSV($data)
{
    $filename = date("Y-m-d").".csv";

    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename=' . $filename);
    header("Content-Transfer-Encoding: UTF-8");

    $f = fopen('php://output', 'a');
    fputcsv($f, array_keys($data[0]));

    foreach ($data as $row) 
    {
        fputcsv($f, $row);
    }
    fclose($f);
}

Try this after opening the file, you want to write in e.g. i want to write a file at below path:

$fp = fopen('csvfiles/myfile.csv','w')

You have to enter headers separately, so for this make an array of headers like:

$csv_fields=array();

$csv_fields[] = 'Enquiry id';
$csv_fields[] = 'Date';
$csv_fields[] = 'Name';
$csv_fields[] = 'Email';
$csv_fields[] = 'Telephone';
$csv_fields[] = 'Customer Request';
$csv_fields[] = 'Special Request';

After making headers add these headers to the file.

fputcsv($fp, $csv_fields);

while($row = mysql_fetch_assoc($details)) {

     fputcsv($fp,$row); 
   } 
fclose($fp);

Also add following headers on top so that file can be viewed easily:

header("content-type: application/force-download");
header('Content-Type: application/csv');
header('Pragma: no-cache');