Tomski Posted May 2, 2009 Share Posted May 2, 2009 Hi, I'm having problems downloading an MySQL table to a csv file. I followed this pretty useful tutorial: http://www.ineedtutorials.com/code/php/export-mysql-data-to-csv-php-tutorial But as opposed to getting a download box the output is dumper into the browser. I assume it's something to do with my header arguments, but having sat here for ages trying a variety of arrangements and getting nowhere I thought I'd ask you guys. I'm getting the same thing on both Mozilla/5.0 and IE7. Here is my code: function exportMysqlToCsv($table,$filename){ // = 'export.csv'){ $csv_terminated = "\n"; $csv_separator = ","; $csv_enclosed = '"'; $csv_escaped = "\\"; $sql_query = "select * from $table"; // Gets the data from the database $result = mysql_query($sql_query); $fields_cnt = mysql_num_fields($result); $schema_insert = ''; for ($i = 0; $i < $fields_cnt; $i++) { $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(mysql_field_name($result, $i))) . $csv_enclosed; $schema_insert .= $l; $schema_insert .= $csv_separator; } // end for $out = trim(substr($schema_insert, 0, -1)); $out .= $csv_terminated; // Format the data while ($row = mysql_fetch_array($result)) { $schema_insert = ''; for ($j = 0; $j < $fields_cnt; $j++) { if ($row[$j] == '0' || $row[$j] != '') { if ($csv_enclosed == '') { $schema_insert .= $row[$j]; } else { $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed; } } else { $schema_insert .= ''; } if ($j < $fields_cnt - 1) { $schema_insert .= $csv_separator; } } // end for $out .= $schema_insert; $out .= $csv_terminated; } // end while //Letting browser know length header("Content-Length: " . strlen($out)); // Output to browser with appropriate mime type, you choose //header("Content-type: text/x-csv"); //header("Content-type: text/csv"); //cache stuff header("Expires: 0"); header("Cache-control: private"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); //header("Content-Type: application/vnd.ms-excel"); //header("Content-type: text/plain"); header("Content-type: text/x-csv"); header("Content-disposition: attachment; filename=" . $filename . ".csv"); echo $out; echo headers_sent(); //testing headers exit; } ?> Cheers, T Quote Link to comment https://forums.phpfreaks.com/topic/156537-solved-download-problem/ Share on other sites More sharing options...
ignace Posted May 2, 2009 Share Posted May 2, 2009 I don't think there is a problem with your code your browser just recognizes the extension and instead of downloading it display's it. The same thing happens when you download a pdf file instead of downloading, the browser reads the file, loads the plugin and display's the pdf in browser. Try adding a different extension and see if that helps P.S. remove the echo headers_sent(); Quote Link to comment https://forums.phpfreaks.com/topic/156537-solved-download-problem/#findComment-824238 Share on other sites More sharing options...
Tomski Posted May 2, 2009 Author Share Posted May 2, 2009 Cheers for the reply. I thought that was the point of the: header("Content-disposition: attachment; filename=" . $filename); So that you could save the out put as a file instead of displaying it. I tried it with a pdf extension and get the same thing. T Quote Link to comment https://forums.phpfreaks.com/topic/156537-solved-download-problem/#findComment-824243 Share on other sites More sharing options...
ignace Posted May 2, 2009 Share Posted May 2, 2009 I was actually referring to an extension that your browser definitly not knows like .foo for example Quote Link to comment https://forums.phpfreaks.com/topic/156537-solved-download-problem/#findComment-824254 Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2009 Share Posted May 2, 2009 What is your code that is using that function? If you are outputting any content before the headers, the headers don't work. Quote Link to comment https://forums.phpfreaks.com/topic/156537-solved-download-problem/#findComment-824259 Share on other sites More sharing options...
Tomski Posted May 2, 2009 Author Share Posted May 2, 2009 I was about to post what's written below. Then I realised that my connectDB has an echo in if the connection fails. Removed it and hey presto. What a tit, I read that header sticky too before posting. Anyway cheers guys. My calling code is below. I'm just trying to get the download to work before I add it to my page so there in no output. I searched around quite a lot before posting this as well so was aware of that common mistake so I don't think it's that. <? include('connectDB.php'); //connect to database require 'exportcsv.inc.php'; $table="arrivals"; // this is the tablename that you want to export to csv from mysql. exportMysqlToCsv($table, 'export.csv'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/156537-solved-download-problem/#findComment-824272 Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2009 Share Posted May 2, 2009 Add the following two lines immediately after your first opening <?php tag (and please use full <?php tags instead of the short <? tag, which may in fact be the cause of your problem) - ini_set ("display_errors", "1"); error_reporting(E_ALL); You may need to temporarily comment out the header() statements to see exactly what is being sent to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/156537-solved-download-problem/#findComment-824278 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.