I have saved an image which is in upload folder, the link is saved in sql table, how should i write a program to download the images from that folder?
for database connection try these codes..
database.php
<?php
try{
$db = new PDO('mysql:host=localhost;dbname=database_name', "username" , "password");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
print "error in connection" . $e->getMessage();
}
now,this php file will access the name of image file stored in database image.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
require_once 'database.php';
$stmt = $db->query("SELECT image_name FROM tablename");
$row = $stmt->fetchall(PDO::FETCH_ASSOC);
foreach ($row as $value) {
$filename = $value['image_name'];
?>
<a href="download.php?id=<?php echo $filename;?>"><?php echo $filename . "<br>";?></a>
<?php }?>
</body>
</html>
download.php
<?php
$filename = $_GET["id"];
header('Content-Type: image/jpg');
header('Content-Disposition: attachment; filename="' . basename($filename) . '";');
readfile ($filename);
exit;