嗨我有这个代码,如果我使用它作为导出数据库的页面..我的问题是当我用它来休息api(苗条)我不知道如何让它工作..我的使用邮递员进行测试
public function export()
{
$qur = mysqli_query($this->con, "SELECT * FROM `tbl_brands`");
// Enable to download this file
$filename = "sampledata.csv";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: text/csv");
$display = fopen("php://output", 'w');
$flag = false;
while($row = mysqli_fetch_assoc($qur)) {
if(!$flag) {
// display field/column names as first row
fputcsv($display, array_keys($row), ",", '"');
$flag = true;
}
fputcsv($display, array_values($row), ",", '"');
}
fclose($display);
}
答案 0 :(得分:0)
Slim基于PSR-7。以下是生成和下载CSV文件(未测试)的示例伪代码:
public function downloadCsvAction(Request $request, Response $response): ResponseInterface
{
$query = mysqli_query($this->con, "SELECT * FROM `tbl_brands`");
$filename = "sampledata.csv";
$stream = fopen('php://memory', 'w+');
$flag = false;
while ($row = mysqli_fetch_assoc($query)) {
if (!$flag) {
// add field/column names as first row
fputcsv($stream, array_keys($row), ",", '"');
$flag = true;
}
fputcsv($stream, array_values($row), ",", '"');
}
rewind($stream);
$response = $response->withHeader('Content-Type', 'text/csv');
$response = $response->withHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
return $response->withBody(new \Slim\Http\Stream($stream));
}