在网站制作过程中,有时需要将数据库里的数据导出下载到本地,这时,我们就可以使用PHP来实现。可以使用PHP的数据库操作和文件处理功能,结合HTTP响应来实现。本文为你提供一个示例代码,演示如何将数据库数据导出为TXT文件并自动下载:

<?php
// 数据库连接信息
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
// 创建数据库连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接是否成功
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 查询数据库数据
$sql = "SELECT * FROM your_table";
$result = $conn->query($sql);
// 检查查询结果是否有数据
if ($result->num_rows > 0) {
// 创建文件句柄,将数据写入文件
$file = fopen("data.txt", "w");
while ($row = $result->fetch_assoc()) {
fputcsv($file, $row); // 使用fputcsv函数将一行数据写入文件,每个字段用逗号分隔
}
fclose($file);
// 设置HTTP响应头,实现自动下载
header("Content-Description: File Transfer");
header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=data.txt");
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Cache-Control: must-revalidate");
header("Pragma: public");
header("Content-Length: " . filesize("data.txt"));
ob_clean();
flush();
readfile("data.txt");
echo "数据已成功导出并下载!";
} else {
echo "没有可导出的数据。";
}
// 关闭数据库连接
$conn->close();
?>代码中的关键解释:
$servername:数据库服务器名称或IP地址。
$username:连接数据库的用户名。
$password:连接数据库的密码。
$dbname:要连接的数据库名称。
your_table:要导出数据的表名。
代码执行后,会将查询结果逐行写入名为"data.txt"的文件中。然后,通过设置HTTP响应头信息,实现自动下载功能。最后,显示相应的提示信息。
-------------------------------------------------------------------------------------
下面介绍一个我早些年写的zblog将文章以txt文件导出的插件代码。注意是早些年,现在zblog已经升级到1.7了。估计是不能用了,很多函数已经不能用了。
<?php
/**
* 插件名称:导出为TXT
* 描述:为当前页面的文章提供导出为TXT文件的功能。
* 版本:1.0
* 作者:您的姓名
* 作者网址:https://www.adminbk.com
* 插件网址:https://www.adminbk.com
*/
// 添加一个自定义按钮到文章页面
function export_to_txt_button() {
global $zbp;
echo 'currenturl.'?export_to_txt=true" class="button">导出为TXT';
}
$zbp->header .= '<link rel="stylesheet" type="text/css" href="'.$zbp->host.'zb_users/plugin/export-to-txt/style.css">';
$zbp->footer .= '<script src="'.$zbp->host.'zb_users/plugin/export-to-txt/script.js"></script>';
$zbp->template->header .= '<script src="'.$zbp->host.'zb_system/script/jquery-3.6.0.min.js"></script>';
$zbp->template->title = '导出为TXT - ' . $zbp->template->title;
$zbp->template->sidebar = '';
// 导出文章内容到TXT文件
function export_to_txt() {
global $zbp;
if (isset($_GET['export_to_txt']) && $_GET['export_to_txt'] === 'true') {
// 获取当前文章
$article = $zbp->GetCurrentArticle();
// 生成TXT内容
$txt_content = $article->Title . "\r\n\r\n" . $article->Content;
// 设置头部信息以强制下载并保存TXT文件
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename=' . $article->TitleSlug . '.txt');
header('Content-Type: text/plain; charset=utf-8');
header('Content-Length: ' . strlen($txt_content));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Expires: 0');
// 输出TXT内容
echo $txt_content;
// 停止脚本执行
exit;
}
}
// 将函数挂钩到适当的事件上
$zbp->actions['export_to_txt_button'] = 'export_to_txt_button';
$zbp->actions['export_to_txt'] = 'export_to_txt';
// 添加按钮到文章页面
$zbp->template->CompileCallBacks['export_to_txt_button'] = 'export_to_txt_button';以上代码将在文章页面添加一个名为"Export to TXT"的按钮,并在点击按钮时将当前文章的标题和内容导出为TXT文件进行下载。您可以将其保存为一个独立的PHP文件(例如`export-to-txt.php`)并将其放置在 `zb_users/plugin/export-to-txt` 目录下。然后,您还需要创建一个 `style.css` 文件和一个 `script.js` 文件,分别用于自定义样式和JavaScript功能。
请注意,这只是一个简单的示例插件,您可能需要根据您的实际需求进行额外的功能扩展和安全性检查。








