public function export() { // 获取导出数量 $count = (int)$this->input->get('export_count'); if ($count <= 0) { dr_json(0, '请输入有效的导出数量'); } // 查询最后的 $count 条记录 $table = SITE_ID . '_app_kami'; $kamis = $this->model->table($table) ->order_by('id', 'desc') ->limit($count) ->get_all(); if (empty($kamis)) { dr_json(0, '没有找到足够的卡密数据'); } // 将卡密写入TXT文件 $filename = '卡密列表_' . date('YmdHis') . '.txt'; $fp = fopen($filename, 'w'); foreach ($kamis as $kami) { fwrite($fp, $kami['kami'] . "\n"); } fclose($fp); // 提供下载 header('Content-Type: text/plain'); header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\""); readfile($filename); unlink($filename); // 删除临时文件 exit; }