download image in folder php mysql code example
Example: download image from mysql using php
//You can save this to test.php and call it with test.php?id=1 for example
<?php
require_once("database.php");
$database = new Database();
$pdo = $database->dbConnection();
if (isset($_GET["id"])) {
$id=(int)$_GET["id"];
} else {
echo "Wrong input";
exit;
}
$q = $pdo->prepare("SELECT * FROM `table_with_image` WHERE `id`=:p_id");
$q->bindparam(":p_id", $id);
$q->execute();
if ($q->rowCount() == 1) {
$row = $q->fetch(PDO::FETCH_ASSOC);
$image = $row['image'];
$database->disconnect();
header("Content-type: image/jpeg");
header('Content-Disposition: attachment; filename="table_with_image_image'.$id.'.jpg"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".strlen($image));
echo $image;
} else {
$database->disconnect();
echo "No image found";
}
exit();