我在MySQL数据库中有数据。我向用户发送一个URL,以获取他们的数据作为CSV文件。
我有链接的电子邮件,MySQL查询等覆盖。
当他们点击链接时,如何弹出从MySQL下载带有记录的CVS的窗口?
我已经有了所有能拿到唱片的资料。我只是不明白如何让PHP创建CSV文件,并让他们下载一个扩展名为. CSV的文件。
我在MySQL数据库中有数据。我向用户发送一个URL,以获取他们的数据作为CSV文件。
我有链接的电子邮件,MySQL查询等覆盖。
当他们点击链接时,如何弹出从MySQL下载带有记录的CVS的窗口?
我已经有了所有能拿到唱片的资料。我只是不明白如何让PHP创建CSV文件,并让他们下载一个扩展名为. CSV的文件。
当前回答
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
function outputCSV($data) {
$output = fopen("php://output", "wb");
foreach ($data as $row)
fputcsv($output, $row); // here you can change delimiter/enclosure
fclose($output);
}
outputCSV(array(
array("name 1", "age 1", "city 1"),
array("name 2", "age 2", "city 2"),
array("name 3", "age 3", "city 3")
));
php: / /输出 函数
其他回答
除了上述内容,你可能还需要补充:
header("Content-Transfer-Encoding: UTF-8");
它在处理包含多种语言的文件时非常有用,比如人名或城市。
已经很好的解决了。我只是把全部代码放进去,这样新手就能得到全部帮助
<?php
extract($_GET); //you can send some parameter by query variable. I have sent table name in *table* variable
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$table.csv");
header("Pragma: no-cache");
header("Expires: 0");
require_once("includes/functions.php"); //necessary mysql connection functions here
//first of all I'll get the column name to put title of csv file.
$query = "SHOW columns FROM $table";
$headers = mysql_query($query) or die(mysql_error());
$csv_head = array();
while ($row = mysql_fetch_array($headers, MYSQL_ASSOC))
{
$csv_head[] = $row['Field'];
}
echo implode(",", $csv_head)."\n";
//now I'll bring the data.
$query = "SELECT * FROM $table";
$select_c = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($select_c, MYSQL_ASSOC))
{
foreach ($row as $key => $value) {
//there may be separator (here I have used comma) inside data. So need to put double quote around such data.
if(strpos($value, ',') !== false || strpos($value, '"') !== false || strpos($value, "\n") !== false) {
$row[$key] = '"' . str_replace('"', '""', $value) . '"';
}
}
echo implode(",", $row)."\n";
}
?>
我已将此代码保存在csv-download.php中
现在看看我是如何使用这些数据来下载csv文件的
<a href="csv-download.php?table=tbl_vfm"><img title="Download as Excel" src="images/Excel-logo.gif" alt="Download as Excel" /><a/>
因此,当我点击链接,它下载文件,而不带我到浏览器上的csv-download.php页面。
最简单的方法是使用一个专门的CSV类,像这样:
$csv = new csv();
$csv->load_data(array(
array('name'=>'John', 'age'=>35),
array('name'=>'Adrian', 'age'=>23),
array('name'=>'William', 'age'=>57)
));
$csv->send_file('age.csv');
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
function outputCSV($data) {
$output = fopen("php://output", "wb");
foreach ($data as $row)
fputcsv($output, $row); // here you can change delimiter/enclosure
fclose($output);
}
outputCSV(array(
array("name 1", "age 1", "city 1"),
array("name 2", "age 2", "city 2"),
array("name 3", "age 3", "city 3")
));
php: / /输出 函数
我知道这个帖子有点老了,但作为将来的参考,以及像我这样的新手:
这里的其他人都解释了如何创建CSV,但忽略了问题的基本部分:如何链接。为了链接到CSV-file的下载,只需链接到.php-file,该文件将响应为. CSV-file。PHP头文件可以做到这一点。这可以实现一些很酷的东西,比如向查询字符串中添加变量并自定义输出:
<a href="my_csv_creator.php?user=23&othervariable=true">Get CSV</a>
my_csv_creator.php可以使用查询字符串中给出的变量,例如使用不同的或自定义的数据库查询,更改CSV的列,个性化文件名等,例如:
User_John_Doe_10_Dec_11.csv