I'm trying to generate a text file that is downloaded (or, ideally, prompted for download) when the user clicks a link.
This chunk of code is supposed to effect this, but instead displays the file contents on the screen (above the body of the page, which is rendered/echoed later in the script).
$file_name = $_REQUEST['employee_id'] . '_' . $data_array[0]['last_name'] . '_' . gmdate('Ymd', gmmktime(0, 0, 0, $month, $start_day, $year)) . '.txt';
$export_file = fopen("text_dumps/$file_name", 'w');
fwrite($export_file, $page->ParmsToTemplate($export, $html->pay_period_admin_export_tmplt));
fclose($export_file);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize("text_dumps/$file_name"));
header("Content-Disposition: attachment; filename=$file_name");
echo file_get_contents("text_dumps/$file_name");
Any idea what I'm doing wrong? Why the text file is just echoing at the top of the page?
I suppose a related question would be, how do I tell it to STOP echoing to the downloaded file, and display any subsequent echoes to the screen? But first things first - getting the file to download.