I am creating a csv file from nested array and it works fine with a download link to the csv file in the localhost, but on live host it won’t download. This is what is in my php file:
The headers declared:
/**
* Declare headers for CSV
*/
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=registration.csv");
header("Pragma: no-cache");
header("Expires: 0");
The Function that outputs the csv file:
/**
* Function will output the scsv file
* @param the csv $data in nested array form array(array("","",""),array("",""...)...)
*/
function outputCSV($data) {
$outstream = fopen("php://output", "w");
function __outputCSV(&$vals, $key, $filehandler) {
fputcsv($filehandler, $vals); // add parameters if you want
}
array_walk($data, "__outputCSV", $outstream);
fclose($outstream);
}
The link that I used in local:
<a href=".../csv/csv.php">Download CSV</a>
Won’t work on Live site. Instead of downloading it just takes me to the csv.php page and outputs the array in a string like this.
…ID,”Coach One”,”Coach Two”,Work,Cell,Email,…
Try hardcoding the csv into the code. Replace these lines with the ones you have to see if your data being passed has bad characters. Maybe specifying the charset will help.
As described here:
http://code.stephenmorley.org/php/creating-downloadable-csv-files/