Jsleshem Jsleshem - 1 year ago 61
HTML Question

Download Excel file from database in PHP

I have a MariaDB database on a site I am making. In this database, I have a bunch of Excel files that are available for download. For simplicity, lets say I have 5 files. The first named

1.xls
, the second named
2.xls
, and so on.

How can I force a download from the database if a user types the text
4.xls
into a form, then clicks a button? Thanks!

Jack

Answer Source

To force the download of your file, link to downloadfile.php?filenum=num

This will be the code in downloadfile.php:

header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename='.$_GET[filenum].'.xls');
header('Pragma: no-cache'); //if the file change, user will always download the last version
readfile('/path/'.$_GET[filenum].'.xls');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download