I am trying to export CSV using PHP. But instead of printing the result it’s printing Resource id #26 in the generated CSV FILE. If I remove exit from end it print’s my whole HTML page content. My code is…
if (isset($_REQUEST['download']) && ($_REQUEST['download'] == "yes")) {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=link_point_report.csv');
$output = fopen('php://memory', 'w+');
fputcsv($output, array('Member Id', 'Customer Name', 'Customer Email'));
$customerListAll = $oAdmFun->linkpoint_check_customer('', '');
$oAdmFun->pr($customerListAll, true);
// if (count($customerListAll) > 0) {
// for ($c = 0; $c < count($customerListAll); $c++) {
// fputcsv($output, array('Sankalp', 'Sankalp', 'Sankalp'));
// }
// }
ob_clean();
flush();
exit($output);
}
That’s because $output is a resource, which is what fopen() returns. You need to use fread() to get its contents.
EDIT: Now I understand what you were asking. To output the contents of your CSV, you need to get the text output from the resource:
And it’s also a good idea to set the content-length header so that the client knows what to expect: