Mahalakshmi S
Mahalakshmi S

Reputation: 1

PHP - Error occurred in export CSV file

When exporting a CSV file in PHP, full page code also export,after that only data export.

Code:

<?php

      header("Content-type: text/csv");
      header("Content-Disposition: attachment; filename=file.csv");
      header("Pragma: no-cache");
      header("Expires: 0");
      $data = array(
          array("data12", "data16", "data17"),
          array("data2", "data33", "data25"),
          array("data31", "data32", "data23")
      );   
      $file = fopen('php://output', 'w');                              
      fputcsv($file, array('Description', 'Click', 'CTR'));      

?>

enter image description here

Upvotes: 0

Views: 1376

Answers (1)

JohnWayne
JohnWayne

Reputation: 663

You should prepare your data for csv file (do not send array). Also I do not know what is your desired result in csv file.

Try this

$data = array(
      array("data12", "data16", "data17"),
      array("data2", "data33", "data25"),
      array("data31", "data32", "data23")
    );
    $csvData = "Your header\n";
    foreach ($data as $row) {
      foreach($row as $dot) {
        $csvData .= $dot.';';
      }
    }
    $csvData = utf8_decode($csvData);
    header('Content-Type: application/csv; charset=UTF-8');
    header('Content-Disposition: attachement; filename="myfile.csv"');
    echo $csvData;
    exit();

Upvotes: 1

Related Questions