【发布时间】:2012-03-21 14:29:27
【问题描述】:
我有一个 HTML 选择表单,它使用在我的数据库中找到的元素填充我的列表,如下所示:
<form action="" method="POST">
<select name="item_select">
<?php
$query = "SELECT * FROM files_table";
$result = mysql_query($query);
while ($row = mysql_fetch_object($result)) {
?>
<option value=<?php echo $row->id; ?> > <?php echo $row->file_name; ></option>
<?php }// end while?>
</select>
<br /><br />
<input name="show_png" type="submit" value="Show" />
<input name="delete" type="submit" value="Delete" />
<input name="download_text" type="submit" value="Download .txt File" />
<input name="download_image" type="submit" value="Download .png File" />
</form>
我有表单的“显示”和“删除”按钮的功能来显示和删除存储在数据库中的内容。但是,我在想办法让用户下载文件时遇到了麻烦。现在,我的基本策略是这样的:
if (isset($_POST['download_text'])) {
// Code to create .txt file from content (omitted).
// File name of content stored in $file_name variable.
// Download file.
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="sample.txt"');
readfile($file_name);
}
这类作品。我的意思是我下载了一个包含内容的文件,但是问题是我还在此内容之后看到了纯文本的 html 表单。有没有办法确保这不会出现?
【问题讨论】: