2

I have the following code for creating thumbnails of the images in the database automatically when the are uploaded.

<?php
include('config.php');
$result = mysql_query("SELECT * FROM photos where caption='cars'");
while($row = mysql_fetch_array($result))
{
    echo '<div id="imagelist">';
    echo '<p><img src="'.$row['location'].'"></p>';
    echo '</div>';
}
?>

Now whenever I click an image in the gallery I want it to open the image in a new tab and give me an option to download, I want the download code which will take in the photo id from the database and then give me the option to download that image only how to do it.

Class
  • 3,149
  • 3
  • 22
  • 31
user2218550
  • 147
  • 1
  • 2
  • 11
  • If it is jpg files then you can follow http://stackoverflow.com/questions/1562523/creating-a-download-link-for-jpg-file-using-php – vishal Mar 28 '13 at 06:33
  • mysql_ functions are deprecated, please read the documentation and use either pdo or mysqli. – Revent Mar 28 '13 at 06:34

1 Answers1

2

Try this :

    $file    = your 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, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;

mysql_* functions are deprecated in new version of php, So use mysqli_* functions OR PDO

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73