0

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;

?>
1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by EvolutionFallen
0

Alright so I fixed the main problem with the empty file almost right after this was posted -- caught the typo on line 44.

But now I'm having the following problem. Once the file is downloaded, and I go to open it with Excel, I receive the following prompts:

"The file you are trying to open [filename] is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file."

Clicking Yes shows the following:

"Cannot read record 1. Continue reporting each error?"

Clicking Yes again shows the following:

"Excel has detected that [filename] is a SYLK file, but cannot load it. Either the file has errors or it is not a SYLK file format. Click OK to try to open the file in a different format."

When the file finally opens after this, the data is not separated into column appropriately. I am delimiting columns with semi-colons, but Excel is not splitting on the semi-colons. Is there a way to force it to? And to make those errors go away?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.