我試圖使用圖像源顯示博客圖像。以下是我如何使用php將圖像插入MYSQL數據庫並存儲到名爲「圖片」的目錄中。插入成功在php中顯示斑點圖像
<form method="post" action="post.php" enctype ="multipart/form-data">
<input type="file" accept="image/png, image/jpeg, image/gif" name="image" id="image"/>
<input type="submit" name="post" value="Post" class="btn btn-primary btn btn-info"/>
</form>
if (isset($_POST['post']) && isset($_FILES['image'])) {
$target_dir = "picture/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$image=basename($_FILES["image"]["name"],".jpg");
$stmt = "INSERT INTO post (image) values (:image)";
$p = $MySQLi_CON -> prepare($stmt);
$results = $p -> execute(array(
":image" => $image
));
}
然而,這裏的問題是,當我嘗試在img-src
來顯示它的圖像鏈接壞了,它不顯示任何內容。我試圖echo $key['image']
,似乎輸出正確的圖像名稱而不是圖像本身
<tbody>
<?php
$query = "SELECT * FROM test";
$data = $MySQLi_CON->query($query);
foreach ($data as $key) {
?>
<tr class="active">
<td><div class = "col-sm-6 col-md-2">
<a class = "thumbnail">
<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($key['image']).'" alt = "Generic placeholder thumbnail"/>';?>
</a>
</div></td>
<td><?php echo $key['image'];?></td> //it gives value such as picture.jpg, image.png
</tr>
<?php
}
?>
</tbody>
一個建議。不要在MySQL中存儲圖像。相反,將圖像的圖像源存儲在數據庫中更加有用。 –
如果您想採取@ResheilAgarwal建議 –
,那麼您喜歡Amazon S3存儲的內容將幫助您存儲圖像您認爲您將BLOB存儲在數據庫中的位置在哪裏?所有你插入的是變量'$ image'的內容,並且它的值只是'basename($ _FILES [「image」] [「name」],「。jpg」)' - 這不是沒有BLOB ... – CBroe