查询数据库内容转csv文件下载

public function csv_GET(){
        $info = $this->sql('xs.nh')->select('nh_id,name')->where(['`from` ='=>'tadu'])->query();//查询数据库内容
        set_time_limit(0);
        $output = fopen('php://output', 'w') or die("can't open php://output");
        $filename = "小说统计" . date('Y-m-d', time());
        header("Content-Type: application/csv");  
        header("Content-Disposition: attachment; filename=$filename.csv");
        $table_head = array('id','小说名');
        fputcsv($output, $table_head);
        foreach ($info as $k=>$v) { 
            //输出内容  
            fputcsv($output, $v);  
        }
        fclose($output);
    }

访问该控制器内的方法,将自动下载csv文件

posted @ 2018-03-07 11:33  白尼玛砸缸  阅读(567)  评论(0编辑  收藏  举报