Help with download button from database

How can i make a button that will download a specific file from my database based off a specific uid, i have partial code, i implemented this code in replacement of a different button and it downloads the correct file, but i need that specific button for something else…

$uid = filter_var($_GET['uid'], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
$doQuery = $mysqli->query("SELECT * FROM consoles WHERE uid=".$uid." LIMIT 1");
while($x = $doQuery->fetch_array()) {
	header("Content-length: ".strlen($x['AppData']));
	header("Content-type: application/octet-stream");
	header("Content-disposition: download; filename=APP-".$x['uid'].".bin");
	echo $x['AppData'];
}

Above is the code i need implemented into a button, anyone able to help? Im assuming i need to create a entire new page.php in order for it to work?

All sorts of ways you could do it - you could use a drop-down within the existing form to set a value which your php would look at to decide which download to create, or you could have a second button on the same form and check for which button was used to submit the form. Or have a second form on your page, with a second submit button that calls either a different php page, or the same one which uses a hidden variable in each form to determine which file is downloaded. I wouldn’t like to say which is better, without more knowledge of the structure of the site and where the buttons are presented, and even then it’s probably down to personal choice.

1 Like

Two submit buttons on the same form have the disadvantage that neither value gets passed to the server if the form is submitted directly using the enter key when some field other than one of the buttons has the focus.

Good point.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.