gpt4 book ai didi

php - 如何下载文件而不是保存 CSV 文件

转载 作者:可可西里 更新时间:2023-11-01 17:08:19 24 4
gpt4 key购买 nike

我正在使用 CSV 扩展名从数据库中导出数据,我想将结果下载为 CSV 文件。我该怎么做?

这是我的代码:

public function actionExportexcel() { 
$con = mysql_connect("localhost", "root", "") or die();
mysql_select_db("fiducial", $con);
$filename = 'uploads/'.strtotime("now").".csv";
$query = mysql_query("SELECT * FROM
(
SELECT employeecode,name,dob,age,sex,employee_relation,company_id
FROM employeedetails
UNION
SELECT employeecode,father_name,father_dob,father_age,father_gender,father_relation,company_id
FROM employeedetails
WHERE father_name IS NOT NULL AND company_id IS NOT NULL
UNION
SELECT employeecode,mother_name,mother_dob,mother_age,mother_gender,mother_relation,company_id
FROM employeedetails
WHERE mother_name IS NOT NULL AND mother_dob IS NOT NULL
)t WHERE t.company_id = '56' ORDER by t.employeecode ") or die(mysql_error());

$num_rows = mysql_num_rows($query);
if($num_rows >= 1)
{
$rows = mysql_fetch_assoc($query);
$seperator = "";
$comma = "";
foreach ($rows as $name => $value)
{
$seperator .= $comma . '' .str_replace('', '""', $name);
$comma = ",";
}
$seperator .= "\n";
$fp = fopen($filename, "w");
fputs($fp, $seperator);
mysql_data_seek($query, 0);
while($rows = mysql_fetch_assoc($query))
{
$seperator = "";
$comma = "";
foreach ($rows as $name => $value)
{
$seperator .= $comma . '' .str_replace('', '""', $value);
$comma = ",";
}
$seperator .= "\n";
fputs($fp, $seperator);
}
echo "Data successfully exported";
fclose($fp);
} else {
echo "No data is Available";
}

}

如何将其下载为 CSV 文件?

最佳答案

您需要为 CSV 文件下载正确设置 HTTP header ,而不是将查询结果写入本地服务器上的 CSV 文件,您需要将其写入 PHP 输出缓冲区 (php://输出):

完整的工作示例:

public function actionExportexcel() { 

$con = mysql_connect("localhost", "root", "") or die();
mysql_select_db("fiducial", $con);
$filename = 'uploads/'.strtotime("now").".csv";
$query = mysql_query("SELECT * FROM
(
SELECT employeecode,name,dob,age,sex,employee_relation,company_id
FROM employeedetails
UNION
SELECT employeecode,father_name,father_dob,father_age,father_gender,father_relation,company_id
FROM employeedetails
WHERE father_name IS NOT NULL AND company_id IS NOT NULL
UNION
SELECT employeecode,mother_name,mother_dob,mother_age,mother_gender,mother_relation,company_id
FROM employeedetails
WHERE mother_name IS NOT NULL AND mother_dob IS NOT NULL
)t WHERE t.company_id = '56' ORDER by t.employeecode ") or die(mysql_error());



$num_rows = mysql_num_rows($query);
if($num_rows >= 1) {
header('Content-Description: Your Download Name ');
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=yourfilename.csv');

$rows = mysql_fetch_assoc($query);
$seperator = "";
$comma = "";
foreach ($rows as $name => $value)
{
$seperator .= $comma . '' .str_replace('', '""', $name);
$comma = ",";
}
$seperator .= "\n";
$fp = fopen('php://output', 'w');
fputs($fp, $seperator);
mysql_data_seek($query, 0);
while($rows = mysql_fetch_assoc($query))
{
$seperator = "";
$comma = "";
foreach ($rows as $name => $value)
{
$seperator .= $comma . '' .str_replace('', '""', $value);
$comma = ",";
}
$seperator .= "\n";
fputs($fp, $seperator);
}

fclose($fp);
} else {
echo "No data is Available";
}

}

关于php - 如何下载文件而不是保存 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30932706/

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