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 ?