gpt4 book ai didi

php - 导出为 CSV PHP 作为下载文件

转载 作者:行者123 更新时间:2023-11-30 23:23:26 25 4
gpt4 key购买 nike

我正在使用 CSV 文件导出 mysql 表。既然它直接存储在驱动器 C: 上而无需任何通知,我如何将其视为下载文件

<?php
$host = 'localhost'; // <-- db address
$user = 'root'; // <-- db user name
$pass = 'root'; // <-- password
$db = 'urs'; // db's name
$table = 'veiwresult'; // table you want to export
$file = 'alaa'; // csv name.
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query(" SELECT ApplicantNum,name, averg,choice FROM veiwresult");
fputcsv($f, array('ApplicantNum','name','averg', 'choice'));
$timestamp = date('Ymd-His');

$f = fopen("C:/mycsv-{$timestamp}.csv", 'w');
// Headers
while($row = mysql_fetch_row($result))
{
fputcsv($f, $row);
}


fputcsv($f, $items_array);
fclose($f);

?>

最佳答案

如果你想存储csv文件然后不点击就打开它,这是不可能的。

如果您只想打开它,请通过

在浏览器窗口中打开它
header('Content-disposition: inline;filename=foobar.csv');
header('Content-Type: text/csv;charset=UTF-8');
echo $csv_content;

其中 $csv_content 是包含 csv 文件内容的字符串。你猫是这样理解的

$csv_content = file_get_contents('c:/mycsv.csv');

关于php - 导出为 CSV PHP 作为下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14491037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com