我在库文件"csv"中的codeigniter中编写了代码。在视图文件中,它有一个名为"导出"的按钮,单击此按钮后,应下载一个名为"cinlist.csv"的csv文件,其中包含一些数据。但是现在当我点击 exprot 按钮时,应该将数据写入文件中,该文件正在浏览器中显示,而不是打开文件"cinlist.csv"并与当前显示在浏览器中的数据一起显示。它实际上是在一台服务器中工作的。当它被迁移到另一台服务器上时,导出功能一直不起作用。
我在这里输入了控制器,视图和模型以及库文件代码:
控制器:
$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;
}
视图:
<?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 (库文件( :
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);
}
}
谁能帮我解决这个问题?
尝试以下代码
// 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"));