In one of the software we are developing, we faced a strange problem in Safari browser on Mac machine. A link on webpage was provided to download a file in .CSV format (server side code written in CakePHP). In Safari, downloaded file was being given the extension .csv.html instead of .csv. So after download file was opening in browser instead of CSV editor / Excel.
While we could not change the extension to .CSV, we were able to change .html at the end to .xls, which solved the problem.
Initially code was like this:
$filename = 'Transaction-'.time();<br /> echo $this->CSV->render($filename);?>
This is the modified code:
$filename = 'Transactions-'.time().'.csv';<br /> header('Content-Type: text/csv; charset=utf-8');<br /> header('Content-Disposition:attachment; filename='.$filename);<br /> echo $this->CSV->render($filename);<br /> exit();
On line 1 – We added ‘.csv’ at end, which will force it to append extension .csv.xls in case of Safari on Mac, on other OS it will continue to save file as .CSV extension
On line 2 – We added this header to support international characters
On line 3 – We added this header to indicate to browser to show Save As dialog box
On last line – We added exit function to confirm that process ends here to not to leave any confusion for server or browser
Now downloaded file gets extension .csv.xls and content is displayed properly in the form of rows and columns.
Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Web development or any other software development assistance please contact us at email@example.com