将页面内容下载为文本文件
Downloading page content as a text file
我在想办法下载由页面上的内容制成的文本文件。在我的例子中,它只是一个纯文本形式的聊天记录。我在管理面板中有一个按钮,您可以使用它来查看聊天记录,另一个按钮应该用于下载所述聊天记录。
日志格式示例:
2019-01-30 08:38:00 This is a chat log.
这就是页面上的所有内容。
我用来查看上述聊天记录的按钮。
<td><a href="chatlog.php?
user='.$rows['user'].'&reportedby='.$rows['reported_by'].'" class="btn btn-
primary btn-sm">View messages</a></td>
<?php session_start();
include 'includes/db.php';
$sender = $_GET['user'];
$receiver = $_GET['reportedby'];
$query = "SELECT * FROM messages WHERE sender='$sender' AND receiver='$receiver'";
$runq = mysqli_query($conn,$query);
while($rows = mysqli_fetch_assoc($runq)) {
echo($rows['date'] . " " . $rows['content']."<br/>");
}
?>
使用 php 代码,您可以像下面的示例那样做
<?php
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=savethis.txt");
// Read your file and print it's content.
// print "This is some text...\n";
`enter code here`
?>
您可以使用 Content-Type
和 Content-Disposition
headers:
将其归档
session_start();
include 'includes/db.php';
$sender = mysqli_real_escape_string(conn, $_GET['user']);
$receiver = mysqli_real_escape_string(conn, $_GET['reportedby']);
$query = "SELECT * FROM messages WHERE sender='$sender' AND receiver='$receiver'";
$runq = mysqli_query($conn, $query);
header('Content-Type: text/plain');
header("Content-Disposition: attachment; filename=chatlogs.txt");
while($rows = mysqli_fetch_assoc($runq)) {
echo($rows['date'] . " " . $rows['content']."\r\n");
}
我还用实际的行结尾替换了 <br/>
,因为它是一个文本文件,如果您使用的是 Unix 系统,您可能必须将其更改为 \n。我添加了 mysqli_real_escape_string 来保护您的查询免受 SQL injection.
我在想办法下载由页面上的内容制成的文本文件。在我的例子中,它只是一个纯文本形式的聊天记录。我在管理面板中有一个按钮,您可以使用它来查看聊天记录,另一个按钮应该用于下载所述聊天记录。
日志格式示例:
2019-01-30 08:38:00 This is a chat log.
这就是页面上的所有内容。
我用来查看上述聊天记录的按钮。
<td><a href="chatlog.php?
user='.$rows['user'].'&reportedby='.$rows['reported_by'].'" class="btn btn-
primary btn-sm">View messages</a></td>
<?php session_start();
include 'includes/db.php';
$sender = $_GET['user'];
$receiver = $_GET['reportedby'];
$query = "SELECT * FROM messages WHERE sender='$sender' AND receiver='$receiver'";
$runq = mysqli_query($conn,$query);
while($rows = mysqli_fetch_assoc($runq)) {
echo($rows['date'] . " " . $rows['content']."<br/>");
}
?>
使用 php 代码,您可以像下面的示例那样做
<?php
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=savethis.txt");
// Read your file and print it's content.
// print "This is some text...\n";
`enter code here`
?>
您可以使用 Content-Type
和 Content-Disposition
headers:
session_start();
include 'includes/db.php';
$sender = mysqli_real_escape_string(conn, $_GET['user']);
$receiver = mysqli_real_escape_string(conn, $_GET['reportedby']);
$query = "SELECT * FROM messages WHERE sender='$sender' AND receiver='$receiver'";
$runq = mysqli_query($conn, $query);
header('Content-Type: text/plain');
header("Content-Disposition: attachment; filename=chatlogs.txt");
while($rows = mysqli_fetch_assoc($runq)) {
echo($rows['date'] . " " . $rows['content']."\r\n");
}
我还用实际的行结尾替换了 <br/>
,因为它是一个文本文件,如果您使用的是 Unix 系统,您可能必须将其更改为 \n。我添加了 mysqli_real_escape_string 来保护您的查询免受 SQL injection.