Create a CSV File for a user in PHP(在 PHP 中为用户创建一个 CSV 文件)
问题描述
我在 MySQL 数据库中有数据.我正在向用户发送一个 URL,以便将他们的数据作为 CSV 文件导出.
I have data in a MySQL database. I am sending the user a URL to get their data out as a CSV file.
我已经涵盖了链接、MySQL 查询等的电子邮件.
I have the e-mailing of the link, MySQL query, etc. covered.
当他们点击链接时,我怎样才能弹出一个窗口来下载带有 MySQL 记录的 CVS?
How can I, when they click the link, have a pop-up to download a CVS with the record from MySQL?
我已经掌握了获取记录的所有信息.我只是不明白如何让 PHP 创建 CSV 文件并让他们下载扩展名为 .csv 的文件.
I have all the information to get the record already. I just don't see how to have PHP create the CSV file and let them download a file with a .csv extension.
推荐答案
试试:
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo "record1,record2,record3
";
die;
等
这是我用来选择性地对 CSV 字段进行编码的代码片段:
Here's a snippet of code I use to optionally encode CSV fields:
function maybeEncodeCSVField($string) {
if(strpos($string, ',') !== false || strpos($string, '"') !== false || strpos($string, "
") !== false) {
$string = '"' . str_replace('"', '""', $string) . '"';
}
return $string;
}
这篇关于在 PHP 中为用户创建一个 CSV 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中为用户创建一个 CSV 文件
基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01