Title: UTF-8 CSV Export Fix
Last modified: July 13, 2020

---

# UTF-8 CSV Export Fix

 *  Resolved [Ivijan-Stefan Stipic](https://wordpress.org/support/users/ivijanstefan/)
 * (@ivijanstefan)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/utf-8-csv-export-fix/)
 * Hi,
 * I updated your code and fixed UTF-8 issues. This solves the problem with German
   and other languages and special characters.
 * I made this update in the file: `/contact-form-cfdb7/inc/export-csv.php`
 * Please double check and add to your new update:
 *     ```
       <?php
       /**
        * CFDB7 csv
        */
   
       if (!defined( 'ABSPATH')) exit;
   
       class Export_CSV{
   
           /**
            * Download csv file
            * @param  String $filename
            * @return file
            */
           public function download_send_headers( $filename ) {
               // disable caching
               $now = gmdate("D, d M Y H:i:s");
               header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
               header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
               header("Last-Modified: {$now} GMT");
   
               // force download
       		header("Content-Description: File Transfer");
       		header("Content-Encoding: UTF-8");
       		header("Content-Type: text/csv; charset=UTF-8");
               header("Content-Type: application/force-download");
               header("Content-Type: application/octet-stream");
               header("Content-Type: application/download");
   
               // disposition / encoding on response body
       		header("Content-Disposition: attachment;filename={$filename}");
               header("Content-Transfer-Encoding: binary");
   
           }
           /**
            * Convert array to csv format
            * @param  array  &$array
            * @return file csv format
            */
           public function array2csv(array &$array, $df){
   
               if (count($array) == 0) {
                   return null;
               }
   
               $array_keys = array_keys($array);
               $heading    = array();
               $unwanted   = array('cfdb7_', 'your-');
   
               foreach ( $array_keys as $aKeys ) {
                   $tmp       = str_replace( $unwanted, '', $aKeys );
                   $heading[] = ucfirst( $tmp );
               }
               fputs( $df, $bom = ( chr(0xEF) . chr(0xBB) . chr(0xBF) ) ); // UTF-8 fix
       		fputcsv( $df, $heading );
   
               foreach ( $array['form_id'] as $line => $form_id ) {
                   $line_values = array();
                   foreach($array_keys as $array_key ) {
                       $val = isset( $array[ $array_key ][ $line ] ) ? $array[ $array_key ][ $line ] : '';
                       $line_values[ $array_key ] = $val;
                   }
                   fputcsv($df, $line_values);
               }
           }
           /**
            * Download file
            * @return csv file
            */
           public function download_csv_file(){
   
               global $wpdb;
               $cfdb        = apply_filters( 'cfdb7_database', $wpdb );
               $table_name  = $cfdb->prefix.'db7_forms';
   
               if( isset($_REQUEST['csv']) && isset( $_REQUEST['nonce'] ) ){
   
                   $nonce =  $_REQUEST['nonce'];
                   if ( ! wp_verify_nonce( $nonce, 'dnonce')) {
   
                       wp_die( 'Not Valid.. Download nonce..!! ' );
                   }
                   $fid         = (int)$_REQUEST['fid'];
                   $heading_row = $cfdb->get_results("SELECT form_id, form_value, form_date FROM $table_name
                       WHERE form_post_id = '$fid' ORDER BY form_id DESC LIMIT 1",OBJECT);
   
                   $heading_row    = reset( $heading_row );
                   $heading_row    = unserialize( $heading_row->form_value );
                   $heading_key    = array_keys( $heading_row );
                   $rm_underscore  = apply_filters('cfdb7_remove_underscore_data', true); 
   
                   $total_rows  = $cfdb->get_var("SELECT COUNT(*) FROM $table_name WHERE form_post_id = '$fid' "); 
                   $per_query    = 1000;
                   $total_query  = ( $total_rows / $per_query );
   
                   $this->download_send_headers( "cfdb7-" . date("Y-m-d") . ".csv" );
                   $df = fopen("php://output", 'w');
                   ob_start();
   
                   for( $p = 0; $p <= $total_query; $p++ ){
   
                       $offset  = $p * $per_query;
                       $results = $cfdb->get_results("SELECT form_id, form_value, form_date FROM $table_name
                       WHERE form_post_id = '$fid' ORDER BY form_id DESC  LIMIT $offset, $per_query",OBJECT);
   
                       $data  = array();
                       $i     = 0;
                       foreach ($results as $result) :
   
                           $i++;
                           $data['form_id'][$i]    = $result->form_id;
                           $data['form_date'][$i]  = $result->form_date;
                           $resultTmp              = unserialize( $result->form_value );
                           $upload_dir             = wp_upload_dir();
                           $cfdb7_dir_url          = $upload_dir['baseurl'].'/cfdb7_uploads';
   
                           foreach ($resultTmp as $key => $value):
                               $matches = array();
   
                               if ( ! in_array( $key, $heading_key ) ) continue;
                               if( $rm_underscore ) preg_match('/^_.*$/m', $key, $matches);
                               if( ! empty($matches[0]) ) continue;
   
                               if (strpos($key, 'cfdb7_file') !== false ){
                                   $data[$key][$i] = $cfdb7_dir_url.'/'.$value;
                                   continue;
                               }
                               if ( is_array($value) ){
   
                                   $data[$key][$i] = implode(', ', $value);
                                   continue;
                               }
   
                               $data[$key][$i] = str_replace( array('&quot;',''','/','\')
                               , array('"',"'",'/','\\'), $value );
   
                           endforeach;
   
                       endforeach;
   
                       echo $this->array2csv( $data, $df );
   
                   }
                   echo ob_get_clean();
                   fclose( $df );
                   die();
               }
           }
       }
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [Arshid](https://wordpress.org/support/users/arshidkv12/)
 * (@arshidkv12)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/utf-8-csv-export-fix/#post-13115638)
 * Please make pull request on GitHub.
 *  Thread Starter [Ivijan-Stefan Stipic](https://wordpress.org/support/users/ivijanstefan/)
 * (@ivijanstefan)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/utf-8-csv-export-fix/#post-13188723)
 * I just do it [https://github.com/arshidkv12/contact-form-cfdb7](https://github.com/arshidkv12/contact-form-cfdb7)

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘UTF-8 CSV Export Fix’ is closed to new replies.

 * ![](https://ps.w.org/contact-form-cfdb7/assets/icon-256x256.png?rev=1619878)
 * [Database Addon for Contact Form 7 - CFDB7](https://wordpress.org/plugins/contact-form-cfdb7/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-cfdb7/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-cfdb7/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-cfdb7/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-cfdb7/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-cfdb7/reviews/)

## Tags

 * [csv](https://wordpress.org/support/topic-tag/csv/)
 * [export](https://wordpress.org/support/topic-tag/export/)
 * [utf8](https://wordpress.org/support/topic-tag/utf8/)

 * 2 replies
 * 2 participants
 * Last reply from: [Ivijan-Stefan Stipic](https://wordpress.org/support/users/ivijanstefan/)
 * Last activity: [5 years, 10 months ago](https://wordpress.org/support/topic/utf-8-csv-export-fix/#post-13188723)
 * Status: resolved