0

I have written a code in codeigniter in a library file 'csv'. In the view file, it has a button called 'Export' and on clicking this button , a csv file named 'cinlist.csv' should be downloaded with some data. But now when I am clicking the exprot button, the data shich should be written into the file is being displayed in the browser instead of the file 'cinlist.csv' being opened and displayed with the data which is currently displayed in the browser. It was actually working in one server. When it was migrated onto another server,export function has not been working.

I have inclued the controller,view and model and library file code here :

controller :

   $this->load->library("csv");

   if (isset($_POST['Export']))
 {

     $data['cin_list'] = '1,ann,34,5,lkg,testschool,testschool,FRcode,kerala' ;
     $listcin=  $data['cin_list'];          
     $data=array(); 
     $n=1;
      foreach($listcin as $item)
      {
         $item['serial_no']=$n;
           $data []=array( 

           $item['serial_no'],
            $item['first_name']. " " .$item['middle_name']. " " .$item['last_name'],
            $item['cin'] ,
            $item['class_id']  ,
            $item['categoryKey']  ,
            $item['school_name']  ,
            $item['school_address'] ,
            $item['franchise_code'], 
            $item['state_subdivision_name']
        );
        $n++;
    }
    $this->csv->export($data,array('serialno','Student Name','Cin','Class','Category','School Name','School Address','Franchise Code','State'),'cinlist.csv');
    exit;

 }

view :

     <?php  if(isset($cin_list) && !empty($cin_list)){ ?> 
        <div align="right"> <button type="submit" class="btn btn-primary" id="Export" name="Export" >Export Excel</button></div>

csv.php (library file) :

    public function export($data=array(), $columns=array(),$file_name='data.csv'){
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename='.$file_name);
    ob_clean();
    $output = fopen('php://output', 'w');
    fputcsv($output, $columns);
    foreach($data as $item){
        fputcsv($output, $item);
    }

    }

Can anyone help me out in this ?

nnnnnn
  • 123
  • 2
  • 2
  • 9
  • Please check https://stackoverflow.com/questions/38910297/how-to-export-array-to-csv-in-codeigniter – kelvin kantaria Dec 16 '19 at 11:30
  • No, its not working...The above code was working fine in another server but when migrated to another server, its not working, – nnnnnn Dec 16 '19 at 11:34

1 Answers1

0

try following code

    // output headers so that the file is downloaded rather than displayed
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=' . $file_name . '.csv');

    // create a file pointer connected to the output stream
    $output = fopen('php://output', 'w');

    // output the column headings
    fputcsv($output, array("City", "Product", "Process", "Price", "Vendor Price", "Brand"));
    fputcsv($output, array("Bangalore", "Product", "Process", "Price", "Vendor Price", "Brand"));
Mohit Rathod
  • 1,057
  • 1
  • 19
  • 33