php Excel 在 2 个地方下载文件,第一个在 htdocs 和下载文件夹中,如何停止在 htdocs 或服务器上下载文件?
Posted
技术标签:
【中文标题】php Excel 在 2 个地方下载文件,第一个在 htdocs 和下载文件夹中,如何停止在 htdocs 或服务器上下载文件?【英文标题】:php Excel downloading file in 2 places, 1st in htdocs and as wellas in downloads folder, how to stop file from downloading in htdocs or on server? 【发布时间】:2021-04-19 05:12:39 【问题描述】:public function genrate_business_XLS_file($export_data)
// create file name
$fileName = 'Business-report-'.date("d-M-Y").'-'.time().'.xlsx';
// load excel library
$this->load->library('excel');
$objphpExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
// set Header
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'USER ID');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'BUSINESS NAME');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'PHONE');
$objPHPExcel->getActiveSheet()->SetCellValue('D1', 'EMAIL');
// set Row
$rowCount = 2;
foreach ($export_data as $val)
//echo"<pre>";print_r($val);die;
$action = '';
if($val['b_status']==0)
$action = 'Rejected Application';
else if($val['b_status']==1)
$action = 'Approved Application';
else if($val['b_status']==2)
$action = 'New Application';
$objPHPExcel->getActiveSheet()->SetCellValue('A' . $rowCount, $val['b_id']);
$objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, $val['b_name']?$val['b_name']:'N/A');
$objPHPExcel->getActiveSheet()->SetCellValue('C' . $rowCount, $val['b_phone']?$val['b_phone']:'N/A');
$objPHPExcel->getActiveSheet()->SetCellValue('D' . $rowCount, $val['b_email']?$val['b_email']:'N/A');
$rowCount++;
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($fileName);
// download file
header("Content-Description: File Transfer");
header("Content-Type: application/vnd.ms-excel");
redirect(site_url().$fileName);
`
【问题讨论】:
【参考方案1】:将文件保存到不在服务器上的php输出
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment;filename="'.$fileName.'"');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('php://output');
【讨论】:
以上是关于php Excel 在 2 个地方下载文件,第一个在 htdocs 和下载文件夹中,如何停止在 htdocs 或服务器上下载文件?的主要内容,如果未能解决你的问题,请参考以下文章
php导出excel表后,打包成压缩包,然后下载到本地如何实现?
如何合并两个excel中的指定数据?如何让2个excel窗口同时在前台显示?