I have an HTML select form that populates my list with elements found in my database like so:
<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>
I have functionality for the “Show” and “Delete” buttons of the form to show and delete the content stored in the database. However, I’m having trouble figuring out a way to have the user download a file. Right now, my basic strategy is this:
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);
}
This sort of works. What I mean by that is I download a file that contains the contents, but what’s wrong is I also see the html form in plain text following this content. Is there someway to ensure this does not show up?
die()after thereadfile.