0

In a wordpress woocommerce installation I need to export some custom order fields to a csv file. Everything works fine, only I don't get the right content in the downloaded file.

Here is my code:

function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
  header('Content-Type: text/csv');
  header('Content-Disposition: attachment; filename="'.$filename.'"');
  $output = fopen( get_temp_dir() . $filename, 'w');
  foreach ($array as $line) { 
    fputcsv( $output, $line );
  }
  fclose($output);
}

The array is something like

$array = array(
  array('Head1', 'Head2', 'Head3', 'Head4'),
  array('Data1', 'Data2', 'Data3', 'Data4'),
  array('Data5', 'Data6', 'Data7', 'Data8')
);

The function is located on a custom admin page, and is triggered by a submit button.

When I press the button, a csv file is generated and written to the temp folder, named as $filename. That file works fine.

The file, which is downloaded automatically, forced by the second header entry in the function, is named correctly ($filename), but the content is the source code of that custom admin page.

I don't need that file to be stored in the temp folder, it's only for now until the automatic download works. I tried to put 'php://temp' or 'php://output' in the fopen function, but that didn't change the content of the download file.

What am I missing?

krabbe
  • 63
  • 1
  • 9
  • 1
    So what content do you get in the downloaded file? – Mark Baker Nov 29 '16 at 12:54
  • 1
    Possible duplicate of [How to create and download a csv file from php script?](http://stackoverflow.com/questions/16251625/how-to-create-and-download-a-csv-file-from-php-script) – Ima Nov 29 '16 at 12:54
  • In the function you provided you're not showing any output. You're just sending the headers as if you're going to send output, then write the file and close it. You never send any of the data. Could you show the code that outputs the data? Alternatively just follow the fclose() with a `readfile(get_temp_dir() . $filename);` to output the content to the browser. – Kalkran Nov 29 '16 at 14:09
  • @MarkBaker The content of the downloaded file is the html source code of that page. – krabbe Dec 05 '16 at 16:26
  • From [link](http://stackoverflow.com/questions/4249432/export-to-csv-via-php) I learned, that I should open a new page, to not having already generated html code in my csv download file. But how do I achieve that? – krabbe Dec 05 '16 at 22:01
  • I found a solution in form of a wordpress developer plugin, which gave me the code base to get my problem solved. If someone using wordpress is struggling with the same problem: [wordpress.org/plugins/csv-download](https://wordpress.org/plugins/csv-download/) – krabbe Dec 07 '16 at 19:54

1 Answers1

0
$data = array(
    array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25),
    array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18),
    array("firstname" => "James", "lastname" => "Brown", "age" => 31),
    array("firstname" => "Patricia", "lastname" => "Williams", "age" => 7),
    array("firstname" => "Michael", "lastname" => "Davis", "age" => 43),
    array("firstname" => "Sarah", "lastname" => "Miller", "age" => 24),
    array("firstname" => "Patrick", "lastname" => "Miller", "age" => 27)
  );

  function cleanData(&$str)
  {
    $str = preg_replace("/\t/", "\\t", $str);
    $str = preg_replace("/\r?\n/", "\\n", $str);
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
  }

  // file name for download
  $filename = "website_data_" . date('Ymd') . ".csv";

  header("Content-Disposition: attachment; filename=\"$filename\"");
  header("Content-Type: application/vnd.csv");

  $flag = false;
  foreach($data as $row) {
    if(!$flag) {
      // display field/column names as first row
      echo implode("\t", array_keys($row)) . "\n";
      $flag = true;
    }
    array_walk($row, 'cleanData');
    echo implode("\t", array_values($row)) . "\n";
  }

  exit;
Ronak
  • 36
  • 5
  • I still get the html source code in my downloaded file, but at least the expected csv-lines are placed after the closed form tag now. – krabbe Dec 05 '16 at 16:32