I have searched for quite some time and do not see something which can solve my issue. I'm working on a page which displays a list of files, and when OnClick, user will be allowed to download that file. File path is stored in MySQL to allow for download.
Currently I am using a while loop with SQL query to retrieve and display(echo) the files.
- logID = Auto increment ID in MySQL
- logDate = DateTime value
fileLink = File path stored in MySQL (E.g C:\www\wamp\file.txt)
while ($row = mysql_fetch_array($result)) { $ID =$row['logID']; $date =$row['logDate']; $file =$row['fileLink']; echo $ID, ' '; echo $date, '<br/>'; }
So on screen right now it shows as follows,
1 2014-01-25 09:33:27
2 2014-01-26 09:37:28
3 2014-01-27 09:38:09
I would like to make the date and time which is "echo $date" into a hyperlink (I have trouble on this) which would allow user to download the file on click. I have a code for file download which is,
<?php
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
The issue is when this download code is on the page, the download box appears right away when the page loads, and offers only the option of downloading the file of "2014-01-27" which is the last retrieved item during the SQL while loop.
I have read that the download code should be on another .php file which would be called when it is required, but I am new to PHP and do not really know how to go about doing it.
Primarily my issues are,
- How to make the echoed items a hyperlink with onclick event to trigger a download.
- How to bind file path to the respective echoed item, as right now $file will store the file path of the last record due to the while loop.
- If the above is possible, and the download code is on another .php file, how do I pass the respective $file variable value to that .php file in order for the method to work.
Thank you in advance and appreciate any help given.