0

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

4
  • Mycode <?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')); ?> Commented Jul 27, 2017 at 8:48
  • isn't that CSV instead of CVS? Commented Jul 27, 2017 at 8:51
  • sorry csv . wrong typed in header Commented Jul 27, 2017 at 9:03
  • Please edit your initial post and add the code as text, not as an image. Furthermore, watch the overall quality of your question. No one is going to answer, if your post has such a low quality. The question will very likely be flagged and might get deleted. Commented Jul 27, 2017 at 9:11

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

4 Comments

Just i export empty csv file means, page coding also export. i can't able to find the mistake
in your example you are trying to write an array in file, and than you are getting error like complete HTML in csv etc.
yes correct.how will i avoid this error when export csv file?. please guide me.
@MahalakshmiS You have to make iteration of your array, or before export define which data structure do you want and than send data to CSV file. You have to explain which data you are sending and what should be your result. This example is working scheme what you have to do to get proper csv file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.