Currently download a CSV file by pressing the button in PHP
I'm creating it, but the file is being created but not downloaded.

I'm investigating a lot, but I can't find a solution.
Please give me your help.

<? php
/ **
 * CSV output
 * /
// include required files
include_once ('hogehuga.php');// Connection information and search functions are included in this.
// execute connection
global $serverName, $conInf;
$conn = sqlsrv_connect ($serverName, $conInf);
$mainData = array ();
$stmt = getHogeList ();
while ($row = sqlsrv_fetch_array ($stmt)) {
    $hogeNo = $row ['hoge_no'];
    $hogeName = mb_convert_encoding ($row ['hoge_name'], 'UTF-8', 'SJIS-WIN');
    $hogeDate = $row ['hoge_date'];
    // add row
    array_push ($mainData, [$hogeNo, $hogeName, $hogeDate]);
sqlsrv_free_stmt ($stmt);
// Close connection
sqlsrv_close ($conn);
// generate file
$fileName = 'HogeFuga.csv';
$csvFile = 'TEMP_' .time () .rand (). '. csv';
$res = fopen ($csvFile, 'w');
foreach ($mainData as $line) {
    mb_convert_variables ('SJIS', 'UTF-8', $line);
    fputcsv ($res, $line);
fclose ($res);
// Start downloading
header ('Content-Type: application/csv');
header ('Content-Disposition: attachment;filename = "'. $fileName. '"');
header ('Content-Length:' .filesize ($csvFile));
readfile ($csvFile);