I am fairly new at this, I was trying to display and download images stored in database as a image path. the display works fine but when I try to download the images from the download button the URL shows the full path of the image but the download dosent happen.
$sql_img = "SELECT * FROM cms
JOIN images ON cms.C_ID = images.C_ID
WHERE B_Name = '$select'";
$sql_img_run = mysqli_query($con, $sql_img);
?>
<table class="display_img">
<tr>
<th>Image ID</th>
<th>Cms ID</th>
<th>Images</th>
</tr>
<?php
while ($row = mysqli_fetch_array($sql_img_run)) {
$img_id = $row['Img_ID'];
$image = $row['Image'];
$cms_id = $row['C_ID'];
// $path = "C:xampphtdocsAiranApi";
$path = "http://localhost/Airan/api/";
?>
<tr>
<td><?php echo $img_id ?></td>
<td><?php echo $cms_id ?></td>
<td><?php echo '<img height="100" src='.$path.$image.'>'?></td>
<td>
<a href="home.php?Image=<?php echo $path.$image; ?>" class="btn-primary download-btn">Download</a>
</td>
</tr>
I have tried various methods but nothing works.
2
Answers
the solutions to this was purely simple. only adding the
download
attribute to the<a>
was downloading the whole page because there was no specific path given to thedownload
attribute.<a download="<?php echo $path.$image; ?>" href="<?php echo $path.$image; ?>" class="btn-primary download-btn">Download</a>
This works like a charm.
Thankyou guys, you gave your precious time to help resolve the issue. I did get to learn a lot of things from you guys. It wouldn't have been possible without you guys.
An example, using hardcoded image paths and nonsense IDs to emulate what you might be drawing from the database.
Use the GET method as you are using a hyperlink with
Image
parameter in querystring.Do not output HTML ( or any content ) before calling
header
function unless you explicitly flush the buffers before.Add the
download
attribute to each hyperlink.