Hello,
I'm trying to create a button that allows the user to d/l info from a DB into a CSV. The button works fine, the download window appears and asks if I want to save/open the csv file. But when the file opens, there is nothing in it, and I don't understand why. If I un-comment the debug section below to test my data variable, the information stored looks fine. Which leads me to believe that the problem is with my header() calls.
Can anyone provide any insight? Thanks in advance:
<?php
require_once("../template.php");
// check to make that the user is logged in
validate_user();
// create a new data object
$dataObj = new data();
if ($iPerm < 2) {
header("Location: ".SITE_URL."/logout.php?op=ip");
}
// Note: $return is a 2-dimensional array
$return = $dataObj->getDataForExport();
// edit array
for($i = 0; $i < count($return); $i++) {
//process data
}
}
// create CSV column headings
$data = "Column;Headings;Here\r\n";
for($i = 0; $i < count($return); $i++) {
$data .= implode(';', $return[$i])."\r\n";
}
/* debug
echo '<pre>';
var_dump($data);
echo '</pre><br />';
*/
// Create file name and output headers for CSV
$filename = "export_".date("Y-m-d_H-i", time());
/////////////////// Basically, everything works fine to this point.
// I know almost nothing about using header() in this way, so this
// is mostly code I got off other sites ///////////////////////////
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// I've tried about 4 diff content-types, including x-msdownload and x-csv
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$filename.csv");
haeder("Pragma: no-cache");
header("Expires: 0");
echo $data;
exit;
?>