yangkai9999 Posted December 10, 2009 Share Posted December 10, 2009 hello,with many help from you, I can export a CSV format data from a mysql table.So far the code works fine in my develop computer. But when I copy to code on another computer, the export file's extension name change from default CSV to xls.Is there anyone knows what the problem is and how to change the code or setting of computer?I install wamp on both computer (win XP, professional) with same setting (one is desktop, other one is laptop)Thank you,code:<?php$host = 'localhost';$user = 'root';$pass = 'Openit4me';$db = 'mytest';$table = 'p1_select';$file = 'export';$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());mysql_select_db($db) or die("Can not connect.");$result = mysql_query("SHOW COLUMNS FROM ".$table."");$i = 0;if (mysql_num_rows($result) > 0) {while ($row = mysql_fetch_assoc($result)) {$csv_output .= $row['Field'].", ";$i++;}}$csv_output .= "\n";$values = mysql_query("SELECT * FROM ".$table."");while ($rowr = mysql_fetch_row($values)) {for ($j=0;$j<$i;$j++) {$csv_output .= $rowr[$j].", ";}$csv_output = substr_replace($csv_output, "\n", -2);}$filename = $file."_".date("Y-m-d_H-i",time());header("Content-type: application/vnd.ms-excel");header("Content-disposition: csv" . date("Y-m-d") . ".csv");header( "Content-disposition: filename=".$filename.".csv");print $csv_output;exit;?> Link to comment Share on other sites More sharing options...
justsomeguy Posted December 10, 2009 Share Posted December 10, 2009 It shouldn't make a different which server it's coming from, the browser think it's an Excel file because that's what the content-type says it is. I use the application/octet-stream content type when I generate CSV files. You also have 2 content-disposition headers, I'm not sure what that's about.I use these headers: header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Last-Modified: ' . gmdate('D,d M YH:i:s') . ' GMT'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-type: application/octet-stream'); header('Content-Disposition: attachment; filename="report.csv"'); Link to comment Share on other sites More sharing options...
yangkai9999 Posted December 10, 2009 Author Share Posted December 10, 2009 It shouldn't make a different which server it's coming from, the browser think it's an Excel file because that's what the content-type says it is. I use the application/octet-stream content type when I generate CSV files. You also have 2 content-disposition headers, I'm not sure what that's about.I use these headers: header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Last-Modified: ' . gmdate('D,d M YH:i:s') . ' GMT'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-type: application/octet-stream'); header('Content-Disposition: attachment; filename="report.csv"');Thank you, I'll try that tomorrow. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.