I'm building a CSV file and serving it up as a downloadable file. Pretty straightforward. Thing is, I get 18 leading empty rows in my csv file. Any ideas? I've stripped the code down to just about nothing - should I be looking for problems somewhere else?
/**
* Implementation of hook_menu
*/
...
$items['providers/dashboard/generate-csv/%/%'] = array(
'title' => t('Export CSV Data'),
'page callback' => 'generate_csv',
'page arguments' => array(3, 4),
'access arguments' => array('administer module_name content'),
'type' => MENU_CALLBACK
);
return $items;
}
/**
* MENU_CALLBACK function
*/
function generate_csv($source_type, $include_unpublished = FALSE) {
// start up headers
$csv_name = $source_type . '-csv_' . date('Y-m-d') . '.csv';
header('Content-Type: text/x-comma-separated-values');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browser
header('Content-Disposition: attachment; filename="' . $csv_name . '"');
// write csv data
$test_output = "test,test,test";
print $test_output;
}
EDIT - Looks like it is an issue with the headers, not the content. If I write the contents of my output to a file, I do not have any extra space at the beginning. Does anyone know:
- What's up with the headers that could be causing the extra spaces? Or..
- How can I ditch any output and start over fresh with my headers?
?>
). Try puttingdrupal_exit()
immediately afterprint $test_output;