Reputation: 487
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.
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,
Thank you in advance and appreciate any help given.
Upvotes: 1
Views: 4200
Reputation: 593
As I understand you want to have multiple links that will provide a log file for that particular date.
You should have one file that will prepare list of the log files as an array OR in your while loop do the following:
echo "<a href='download.php?logId={$row['logID']}'>{$row['logDate']}</a>";
Then you should create download.php (your download script that you already have prepared) that will serve file based on logId
GET parameter (another query to the database to get filePath
).
Upvotes: 0
Reputation: 218877
I have read that the download code should be on another .php file which would be called when it is required
That is indeed the case. Thinking of this from a request/response perspective, your PHP code essentially needs to respond to two different kinds of requests:
These two things would be in two different PHP files. Let's say that the first one is called list.php
. It would just show the HTML content for the list of files. It could be something as simple as what you have:
while ($row = mysql_fetch_array($result)) {
$ID =$row['logID'];
$date =$row['logDate'];
$file =$row['fileLink'];
echo $ID, ' ';
echo $date, '<br/>';
}
You mention that you want the date to be the hyperlink, correct? Then just surround it with the hyperlink markup. Something like this:
echo '<a href="download.php?id=' . $ID . '">' . $date . '</a>', '<br />';
Notice I did a couple of things here. Not only is the date value now surrounded by a hyperlink, but the hyperlink includes a couple of components:
download.php
, which I'm going to assume is the name of the second page. It can be anything you decide, of course.$ID
, which is the value being sent to the second page so it knows which document was clicked. This is a query string value on the URL and would be obtained on the second page by $_GET['id']
. NOTE: This value can be changed by the user, do not rely on it as a security measure. Always validate that the user has permission to download the file they've requested on the second page itself.That second page, download.php
, would include the code you have for writing the file to the output. The header information, the file contents, etc. In that page you'd get the ID of the file being requested ($_GET['id']
), fetch that one file from the database or the file system or wherever it is, and send it to the user with the code you have.
That way the user is presented with the first page and then can click on any link(s) they want to make request(s) to the second page for the file(s) they want.
Upvotes: 1
Reputation: 114
As per your issues 1 and 2:
while ($row = mysql_fetch_array($result)) {
$ID =$row['logID'];
$date =$row['logDate'];
$file =$row['fileLink'];
echo $ID, ' ';
echo '<a href="'.$file.'">'.$date.'</a><br/>'; // Hyperlink to file.
}
And for the 3rd issue you can try
while ($row = mysql_fetch_array($result)) {
$ID =$row['logID'];
$date =$row['logDate'];
$file =$row['fileLink'];
echo $ID, ' ';
echo '<a href="yourfile.php?file='.$file.'">'.$date.'</a><br/>';
}
Upvotes: 0