php导出csv文件

//导出csv格式的表格
public function exportcsv() {
$keyword=trim(I('keyword'));
$where['nickname|openid'] = array('like','%'.$keyword.'%');
$data= M('user')->where($where)->select();
$str = "序号,姓名,关注,openid,昵称,性别,城市,国家,省份,语言,头像,关注时间,unionid,备注,创建时间,用户组\r\n";
foreach($data as $key=>$value){
$id = $value['id'];
$name = $value['name'];
$subscribe = $value['subscribe'];
$openid = $value['openid'];
$nickname = $value['nickname'];
$sex = $value['sex'];
$city = $value['city'];
$country = $value['country'];
$province = $value['province'];
$language = $value['language'];
$headimgurl = $value['headimgurl'];
$subscribe_time = $value['subscribe_time'];
$unionid = $value['unionid'];
$remark = $value['remark'];
$ctime = date('Y-m-d H:i:s',$value['ctime']);
$groupid = $value['groupid'];
$str .= "".$id.",".$name.",".$subscribe.",".$openid.",".$nickname.",".$sex.",".$city.",".$country.",".$province.",".$language.",".$headimgurl.",".$subscribe_time.",".$unionid.",".$remark.",".$ctime.",".$groupid."\r\n";
//$str .="".$name.",".$section1.",".$phone.",".$section_name.",".$ordertime.",".$content.",".$addtime.",".$idcard."\r\n";
}
$filename = date('Ymd').'.csv'; //设置文件名
if(filesize($file)>1024){
$file = getcwd().'/aaa.csv';
//dump($file);exit;
file_put_contents($file,$str);
//dump($str);exit;
if (file_exists($file)) {
$filesize=filesize($file)+1000;

header('Content-Description:File Transfer');
header('Content-type:'.csv);
header('Content-Transfer-Encoding:binary');
header("Accept-Ranges: bytes");
header('Expires:0');
header('Cache-Control:must-revalidate');
header('Pragma:public');
header("Content-Length:".$filesize);
header('Content-Disposition: attachment; filename='.$filename);
//打开文件
$fp = fopen($file, "rb");
//dump($fp);exit;
//设置指针位置
fseek($fp,0);
while (!feof($fp)) {
//虚幻输出
//设置文件最长执行时间
set_time_limit(0);
print (fread($fp, 1024 * 150)); //输出文件
flush(); //输出缓冲
ob_flush();
}
fclose($fp);

unlink($file);
exit ();
}else{
echo "err";die();
}
}else{
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=".$filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $str;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值