从我的数据库中检索BLOB图像时,它只渲染约1/5的图像并停止。
示例:
fetchavatar.php - 从数据库中检索图像。
// Start session.
session_start();
$id = $_SESSION['id'];
$stmt = $con->prepare("SELECT * FROM avatars WHERE user_id = ?");
$stmt->bindParam(1, $id);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$fetchedrows = $stmt->rowCount();
$_SESSION['avatar'] = base64_encode($result['data']);
$avatar = "<img src='data:image/jpeg;base64,". $_SESSION['avatar'] . "'>";
upload.php - 将图片插入数据库。
include "fetchavatar.php";
$name = $_FILES['avatar']['name'];
$temp = $_FILES['avatar']['tmp_name'];
$size = $_FILES['avatar']['size'];
$type = $_FILES['avatar']['type'];
$fp = fopen($temp, 'r');
$img = fread($fp, filesize($temp));
fclose($fp);
if($fetchedrows) {
$stmt = $con->prepare("UPDATE avatars SET name = ?, type = ?, size = ?, data = ? WHERE user_id = ?;");
} else {
$stmt = $con->prepare("INSERT into avatars (name, type, size, data, user_id) VALUES(?, ?, ?, ?, ?);");
}
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $type);
$stmt->bindParam(3, $size);
$stmt->bindParam(4, $img);
$stmt->bindParam(5, $id);
$stmt->execute();
profile.php - 显示个人资料页面。
<html>
...
<?php include "fetchavatar.php"; ?>
...
<body>
<div id="profile_div">
<h1>Profile Page</h1>
<?php echo $avatar ?>
...
</body>
</html>
答案 0 :(得分:4)
将您的数据类型从blob
更改为longblob
.. =)