I need to export data in one table in a csv file. I’m able to get the data fine but the CSV file is not being generated by the browser.
My code is like this: its the problem with headers. I’m getting only the output with commas separated values but not getting csv file.
/* Converting data to CSV */
public function CSV_GENERATE($getTable)
{
ob_clean();
global $wpdb;
$field='';
$getField ='';
if($getTable){
$result = $wpdb->get_results("SELECT * FROM $getTable");
$requestedTable = mysql_query("SELECT * FROM ".$getTable);
// echo "hey";die;//var_dump($result);die;
$fieldsCount = mysql_num_fields($requestedTable);
for($i=0; $i<$fieldsCount; $i++){
$field = mysql_fetch_field($requestedTable);
$field = (object) $field;
$getField .= $field->name.',';
}
$sub = substr_replace($getField, '', -1);
$fields = $sub; # GET FIELDS NAME
$each_field = explode(',', $sub);
$csv_file_name = $getTable.'_'.date('Ymd_His').'.csv';
# CSV FILE NAME WILL BE table_name_yyyymmdd_hhmmss.csv
# GET FIELDS VALUES WITH LAST COMMA EXCLUDED
foreach($result as $row){
for($j = 0; $j < $fieldsCount; $j++){
if($j == 0) $fields .= "n"; # FORCE NEW LINE IF LOOP COMPLETE
$value = str_replace(array("n", "nr", "rn", "r"), "t", $row->$each_field[$j]); # REPLACE NEW LINE WITH TAB
$value = str_getcsv ( $value , ",", """ , ""); # SEQUENCING DATA IN CSV FORMAT, REQUIRED PHP >= 5.3.0
$fields .= $value[0].','; # SEPARATING FIELDS WITH COMMA
}
$fields = substr_replace($fields, '', -1); # REMOVE EXTRA SPACE AT STRING END
}
header("Content-type: text/x-csv"); # DECLARING FILE TYPE
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=".$csv_file_name); # EXPORT GENERATED CSV FILE
header("Pragma: no-cache");
header("Expires: 0");
header("Content-type: application/x-msdownload");
//header("Content-Disposition: attachment; filename=data.csv");
return $fields;
}
This is working perfectly now. we can use this as a plugin. I modified this post. thanks to sruthi sri.
Hope this helps some one π
I’m a late bloomer, but made a small ‘improvement’ to the code you guys worked on and would like to share. If code pasted in the main plugin .php file you don’t need to go through the 3 steps. Just change the values at the bottom of the script as required. I like to keep it neat though with plenty of comments for you guys.
For beginners who might need this and to add flexibility for everyone to use:
define('MY_PLUGIN_DIR',
plugin_dir_path(__FILE__));
require_once(PARTS_MY_PLUGIN_DIR . '/databasestuff/table_to_csv.php')
your_plugin_directory/databasestuff/table_to_csv.php
save the following class and change the last few lines as required.Make adjustments to the last few lines
Keep in mind:
Just making some small adjustments to @Developer since it wasn’t quite pulling up in the admin, or downloading the csv. But now it will π :
Just create a file called csv_export.php put it in a plugins/csv_export/ and you’re gtg!
I’m not certain, but there are a few things it might be.
Your braces don’t match – you’re missing a closing
}
somewhere.You’re not actually sending the generated content anywhere, unless you’re doing it in the calling routine? Maybe you mean
echo $fields;
, notreturn $fields;
?You’re calling
ob_clean()
– do you have output buffering turned on? Perhaps you meanob_end_clean()
– to discard the buffer and turn buffering off?I’m creating a CSV for export; it’s working with just the following headers:
In terms of differences with your calls:
Content-Type
headersContent-Transfer-Encoding
I don’t know that any of those differences are related to your problem, I’m just listing them in case they help.
I met the same problem. I found that the problem is not the headers code, but the link you click to export. You need to add an argument named “noheader”Γ―ΒΌΒ
I replaced my code with exactly the same headers code mentioned in this question, and it does work as well.
My export process code looks like:
The last two headers tell the browser not to cache the exported content.