PHP利用缓存灺导出CVS文件

$model_user = new \app\admin\model\User();
            //搜索条件
            $phone             = input('get.phone', "");
            $start_time        = input('get.start_time', "");
            $end_time          = input('get.end_time', "");
            $is_lock           = input('get.is_lock/d', 0);
            $export_app_number = input('get.export_app_number', 0);

            $export_nick_name   = input('get.export_nick_name', 0);
            $export_phone       = input('get.export_phone', 0);
            $export_status      = input('get.export_status', 0);
            $export_balance      = input('get.export_balance', 0);
            $export_create_time = input('get.export_create_time', 0);

            $where["is_delete"] = 0;
            if ($phone != "") {
                $where["phone"] = $phone;
            }
            if ($start_time != "" && $end_time != "") {
                $where['create_time'] = array('between', [$start_time, $end_time]);
            } else {
                if ($start_time != "") {
                    $where["create_time"] = ['>=', $start_time];
                }
                if ($end_time != "") {
                    $where["create_time"] = ['<=', $end_time];
                }
            }
            if ($is_lock == "0" || $is_lock == "1") {
                $where["is_lock"] = $is_lock;
            }
            //导出选项条件
            $field    = "";
            $headList = array(); //$headList = ['id', '用户昵称', '应用id', '手机号', '创建时间'];
            if ($export_app_number == "true") {
                $headList[] = '用户id';
                $field      = $field . "app_number,";
            }
            if ($export_nick_name == "true") {
                $headList[] = '用户昵称';
                $field      = $field . "nick_name,";
            }
            if ($export_phone == "true") {
                $headList[] = '手机号';
                $field      = $field . "phone,";
            }
            if ($export_status == "true") {
                $headList[] = '状态';
                $field      = $field . "is_lock,";
            }
            if ($export_balance == "true") {
                $headList[] = '零钱余额';
                $field      = $field . "balance,";
            }
            if ($export_create_time == "true") {
                $headList[] = '创建时间';
                $field      = $field . "create_time,";
            }
            $field = substr($field, 0, -1);
            if ($field == '') {
                exit;
            }

            //分页数据
            $start    = 0;
            $limit    = 500000;
            $order_by = "";
            $dataList = $model_user->get_all_user($where, $start, $limit, $order_by, $field);
            if ($dataList && $export_status == "true") {
                foreach ($dataList as $k => $v) {
                    if ($v["is_lock"] == 0) {
                        $dataList[ $k ]["is_lock"] = "正常";
                    } else {
                        $dataList[ $k ]["is_lock"] = "已拉黑";
                    }
                }
            }
            $fileName  = '导出用户' . date("YmdHis");
            $exportUrl = "php://output";

            //导出用户
            set_time_limit(0);//防止超时
            ini_set("memory_limit", "512M");//防止内存溢出
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="' . $fileName . '.csv"');
            header('Cache-Control: max-age=0');
            //打开PHP文件句柄,php://output 表示直接输出到浏览器,$exportUrl表示输出到指定路径文件下
            $fp = fopen($exportUrl, 'a');

            //输出Excel列名信息
            foreach ($headList as $key => $value) {
                //CSV的Excel支持GBK编码,一定要转换,否则乱码
                $headList[ $key ] = iconv('utf-8', 'gbk', $value);
            }
            //将数据通过fputcsv写到文件句柄
            fputcsv($fp, $headList);
            //计数器
            $num = 0;
            //每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
            $limit = 100000;
            //逐行取出数据,不浪费内存
            $count = count($dataList);
            for ($i = 0; $i < $count; $i++) {

                $num++;
                //刷新一下输出buffer,防止由于数据过多造成问题
                if ($limit == $num) {
                    ob_flush();
                    flush();
                    $num = 0;
                }

                $row = $dataList[ $i ];
                foreach ($row as $key => $value) {
                    $row[ $key ] = iconv('utf-8', 'gbk', $value)."\t";
                }
                fputcsv($fp, $row);
            }

原文地址:https://www.cnblogs.com/lookingbackagin/p/14764826.html