我是PHP的新手,我在显示数据库中的图像时遇到问题。关于这一点有很多文章和帖子,但没有什么对我有用。
图像在数据库中正确保存,我可以看到
这是我的代码:
saveimage.php
function uploadImage($name, $image, $type) {
require 'db_config.php';
/* @var $DBH PDO */
try {
$STH = $DBH->prepare("INSERT INTO images (name,image,type) VALUES (:name ,:image,:type)");
$STH->bindParam(':name', $name);
$STH->bindParam(':image', $image);
$STH->bindParam(':type', $type);
$STH->execute();
$DBH = null;
} catch (Exception $ex) {
echo "Error: " . $ex->getMessage();
}
}
showimage.php
require 'db_config.php';
/* @var $DBH PDO */
if (isset($_GET['id'])) {
try {
$id = $_GET['id'];
$STH = $DBH->prepare("SELECT name,image,type FROM images WHERE id=" . $id);
$STH->execute();
$image = $STH->fetch(PDO::FETCH_OBJ);
$DBH = null;
header("Content-Type: " . $image->type);
header("Content-Length: " . filesize($image->name));
echo $image->image;
} catch (Exception $ex) {
echo "Error: " . $ex->getMessage();
}
}
的index.php:
<div>
<img src="showimage.php?id=5" width="200" height="200">
</div>