PHP: PDO + CSV export not downloading (headers issue?)



PHP Snippet 1:

$fp = fopen("php://output", "w");

PHP Snippet 2:

$tmpfname = tempnam("/", "");
$fp = fopen($tmpfname, "w");

PHP Snippet 3:

fclose($fp);

PHP Snippet 4:

fclose($fp);
readfile($tmpfname);
unlink($tmpfname);

PHP Snippet 5:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="export.csv"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('export.csv'));
readfile($csvFile);
exit;
`put this after` fclose($fp);