View Full Version : problem exporting data to csv file

09-17-2011, 12:42 PM
Hi all,

this is the code i'm using at the moment which is at the bottom of a webpage

// Open file export.csv.
$f = fopen("export.csv", "w");

// Put all values from $out to export.csv.
fputs($f, $out);

$filename = "StatisticalAnalysis-" . gmdate("Y-m-d-H-i-s") . ".csv";

header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=" . $filename);

The variable named $out is a long line of text in csv format. I've echoed that variable and the code is all good.

When a checkbox in a form further up the page is checked the code creates and fills in the $out variable and then carries out the code above.

The problem i have is that when the csv save dialog comes up and the file is saved, all it contains is a copy of the html code which is on that page, not the info stored in the $out variable.

Can anyone offer any advice on what the problem is?


09-17-2011, 03:44 PM
The question is where did you echo the $out? Just after you set the variable or just before you wrote to the file. You should do both to see if $out is being changed.

09-18-2011, 06:22 AM
Yes, show the code for the bit where you create the content for $out and it might come clear?