xml - xls file php download is in a different format than specified by the file extension -
i'm struggling xml xls code in php.
have php code exports mysql data xml , have code writes xml xls.
works fine, except when open xls file, error:
"the file trying open in different format specified file extension. verify file not corrupted , trusted source before opening file. want open file now?"
my code:
<?php $file = 'filename'; $url = "$file.xml"; // xml file header( "content-type: application/vnd.ms-excel" ); header( "content-disposition: attachment; filename=$file.xls" ); // xls file if (file_exists($url)) { $xml = simplexml_load_file($url); echo 'voornaam'."\t" . 'achternaam'."\t" . 'geslacht'."\t" . 'instrument'."\t\n"; foreach($xml->lid $lid) { echo $lid->voornaam."\t" . $lid->achternaam."\t" . $lid->geslacht."\t" . $lid->instrument."\n"; } } ?>
i found has mime_types
https://filext.com/faq/office_mime_types.php
the header("content-type: application/vnd.ms-excel");
older versions of excel. when use xlsx type, file won't open @ all.
like said, code runs fine, downloads excel file in xls format computer. how can open without error?
i upgrade file xlsx format if someonw knows how open file in format.
well, header and filename both saying "this xls file" - isn't, it's tsv file (tab-separated values). yes, message accurate: file extension misleading.
to write actual, valid xls (or xlsx), easiest use library - both these formats complex , tricky. i've had best results using phpexcel (example code).
Comments
Post a Comment