我有一个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>
Run Code Online (Sandbox Code Playgroud)
我有表单的"显示"和"删除"按钮的功能,以显示和删除存储在数据库中的内容.但是,我无法找到让用户下载文件的方法.现在,我的基本策略是:
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);
}
Run Code Online (Sandbox Code Playgroud)
这种作品.我的意思是我下载一个包含内容的文件,但是错误的是我在这个内容后面也看到了纯文本的html表单.是否有某种程度可以确保这不会出现?