Menu

Add Topic

Forum

waiting answer January 20, 2021

Export to CSV via PHP

I have an array of id, name, and email. I want to export this to CSV. Anyone Please help me

Answers
January 20, 2021

     <?php
         $delimiter = ",";
         $filename = "test.csv";
         //create a file pointer
         $f = fopen('php://memory', 'w');
         $fields = array('id', 'Name' ,'Email');

         fputcsv($f, $fields, $delimiter);

         $lineData = array('10', 'Ganesh','ganesh@gmail.com');
         fputcsv($f, $lineData, $delimiter);

         fseek($f, 0);        
         //set the header to download the file
         header('Content-Type: text/csv');
         header('Content-Disposition: attachment; filename="' . $filename . '";');   
         //Output the remaining data
         fpassthru($f);
    ?>

0 0

Please Login to Post the answer

Leave an Answer