下载csv文件

I have a function that is called via ajax which creates a csv file, the response code coming back is 200 but the file doesn't download - the function I have is:

private function createCvsObject($array){
    $f = fopen('ActiveReports.csv', 'w');

    foreach($array as $projectNumber=>$project){
        fputcsv($f, $project, ',');
    }

    fclose($f);
}

As far as I know, it creates the csv file - but it wont download it. Is there some step I am missing in here so that it will auto download the file after creating it?

Just add

header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=ActiveReports.csv');
header('Pragma: no-cache');
readfile("ActiveReports.csv");

after fclose($f);