I have an option on my site to export a set of results to an Excel file, but Im getting an error that seems to relate to the file extension being .csv so i thought to change it to .xls and the same error appears.
This is my full code for that page
session_start();
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Transfer-Encoding: binary");
header("Content-Type: application/csv");
function cleanData(&$str)
{
if($str == 't') $str = 'TRUE';
if($str == 'f') $str = 'FALSE';
if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {
$str = "'$str";
}
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
$str = mb_convert_encoding($str, 'UTF-16LE', 'UTF-8');
}
$filename = "website_data_" . date('Ymd') . ".csv";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: text/csv; charset=UTF-16LE");
$out = fopen("php://output", 'w');
$flag = false;
When I click export the first warning is -
The file format and extension of âwebsite_data_20180116-21.csvâ donât match. The file could be corrupted or unsafe. Unless you trust its source, dont open it. Do you want to open it anyway.
So I click âYesâ to open it anyway, and then I get
Excel has detected that âwebsite_data_20180116-21.csvâ is a SYLK file, but cannot load it. Either the file has errors or it is not a SYLK file format.
So I click OK to open and if I then keep that empty Excel file open, I can then click Export again and the data will then create an excel file no problem. but I have to have excel open for it to load without these messages.
I read somewhere its because in the first column I have a field name called âIDâ, but not sure and how would I change that to say âidâ just to output it to the excel file.