[英]PHP - Mysql blob to image
I've read many posts but for some reason it still doesn't work. 我读了很多帖子,但由于某种原因它仍然无效。 As in the title, I want to display image in the website which is stored in MySQL
as MEDIUM BLOB
. 在标题中,我想在网站中显示图像,该图像作为MEDIUM BLOB
存储在MySQL
中。 Here is the code which uploads the image: 以下是上传图片的代码:
if (isset($_FILES["fileToUpload"]["tmp_name"])) {
if(getimagesize($_FILES["fileToUpload"]["tmp_name"]) == FALSE)
echo '<p style="color: red" >No file selected</p>';
else {
echo '<p style="color: red" >SELCETED</p>';
$image= addslashes($_FILES["fileToUpload"]['tmp_name']);
//$imageName= addcslashes($_FILES["fileToUpload"]['name']);
$image = file_get_contents($image);
$image = base64_encode($image);
}
}
if (isset($_POST['trescText']) )
$trescText=$_POST['trescText'];
if($titleText != ""&& $trescText != "") {
$stmt = $conn->prepare("INSERT INTO blog (title,cykl,tresc, image) VALUES('$titleText','$cyklText','$trescText','$image')");
$stmt->execute();
header('Location: addPost.php');
}
$conn->close();
And the code which displays it: 以及显示它的代码:
<?php
$stmt = $conn->prepare("SELECT image FROM blog WHERE id='98'");
$stmt->execute();
$stmt->bind_result($image);
while ($stmt->fetch()) {
// echo '<img src="data:image;base64,'$image' "/>';
echo '<img src="data:image/jpeg;base64,'.base64_encode($image) .'" />';
}
?>
The problem is that instead of orginal image I get this: 问题是,而不是原始图像我得到这个:
You base64-encoded it twice: once when inserting it into the database, and again when sending it to the browser. 您对它进行了64次编码:一次将其插入数据库,一次将其发送到浏览器。
Base64-encoding has a tangible result; Base64编码有一个明显的结果; that is, it transforms the data. 也就是说,它会转换数据。 It is not temporary. 这不是暂时的。 The data in your database are the base-64 representation of your image's bytes, and that's the same data that you pull out later with SELECT
. 数据库中的数据是图像字节的base-64表示形式,这与稍后使用SELECT
提取的数据相同。
So you only want to do the encoding once. 所以你只想做一次编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.