217 php csv download http-headers
我在MySQL数据库中有数据.我正在向用户发送一个URL,以将其数据作为CSV文件输出.
我有电子邮件的链接,MySQL查询等.
当他们点击链接时,我怎么能弹出一个下载带有MySQL记录的CVS?
我已经掌握了获取记录的所有信息.我只是不知道如何让PHP创建CSV文件并让他们下载扩展名为.csv的文件.
And*_*nko 462
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")
));
Run Code Online (Sandbox Code Playgroud)
Ole*_*hay 280
尝试:
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo "record1,record2,record3\n";
die;
Run Code Online (Sandbox Code Playgroud)
等等
编辑:这是我用来选择编码CSV字段的代码片段:
function maybeEncodeCSVField($string) {
if(strpos($string, ',') !== false || strpos($string, '"') !== false || strpos($string, "\n") !== false) {
$string = '"' . str_replace('"', '""', $string) . '"';
}
return $string;
}
Run Code Online (Sandbox Code Playgroud)
Xeo*_*oss 18
这是@Andrew发布的php.net函数的改进版本.
function download_csv_results($results, $name = NULL)
{
if( ! $name)
{
$name = md5(uniqid() . microtime(TRUE) . mt_rand()). '.csv';
}
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='. $name);
header('Pragma: no-cache');
header("Expires: 0");
$outstream = fopen("php://output", "wb");
foreach($results as $result)
{
fputcsv($outstream, $result);
}
fclose($outstream);
}
Run Code Online (Sandbox Code Playgroud)
它非常易于使用,并且适用于MySQL(i)/ PDO结果集.
download_csv_results($results, 'your_name_here.csv');
Run Code Online (Sandbox Code Playgroud)
exit()如果您已完成该页面,请记住在调用此项之后.
Sta*_*tan 16
除了已经说过的所有内容之外,您可能还需要添加:
header("Content-Transfer-Encoding: UTF-8");
Run Code Online (Sandbox Code Playgroud)
在处理包含多种语言的文件(例如人名或城市)时,它非常有用.
小智 9
我知道,这个帖子有点旧,但是为了将来的参考和我自己的新手:
这里的其他人都解释了如何创建CSV,但错过了问题的基本部分:如何链接.为了链接到CSV文件的下载,您只需链接到.php文件,该文件又响应为.csv文件.PHP标题就是这样做的.这可以实现很酷的东西,比如在查询字符串中添加变量并自定义输出:
<a href="my_csv_creator.php?user=23&othervariable=true">Get CSV</a>
Run Code Online (Sandbox Code Playgroud)
my_csv_creator.php可以使用查询字符串中给出的变量,例如使用不同的或自定义的数据库查询,更改CSV的列,个性化文件名等,例如:
User_John_Doe_10_Dec_11.csv
Run Code Online (Sandbox Code Playgroud)
创建您的文件,然后使用正确的标题返回对它的引用以触发另存为 - 根据需要编辑以下内容.将您的CSV数据放入$ csvdata.
$fname = 'myCSV.csv';
$fp = fopen($fname,'wb');
fwrite($fp,$csvdata);
fclose($fp);
header('Content-type: application/csv');
header("Content-Disposition: inline; filename=".$fname);
readfile($fname);
Run Code Online (Sandbox Code Playgroud)
这是一个使用PDO并包含列标题的完整工作示例:
$query = $pdo->prepare('SELECT * FROM test WHERE id=?');
$query->execute(array($id));
$results = $query->fetchAll(PDO::FETCH_ASSOC);
download_csv_results($results, 'test.csv');
exit();
function download_csv_results($results, $name)
{
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='. $name);
header('Pragma: no-cache');
header("Expires: 0");
$outstream = fopen("php://output", "wb");
fputcsv($outstream, array_keys($results[0]));
foreach($results as $result)
{
fputcsv($outstream, $result);
}
fclose($outstream);
}
Run Code Online (Sandbox Code Playgroud)