我编写了一个代码,您可以在其中从计算机中选择图像并将其上传到数据库。由于某种原因,在我上传它之后(它已正确上传到数据库中),显示的图像不是我上传的图像,而是一个小图像,显示它无法从数据库获取图像。有人能帮我吗??这是代码:
index.php:
<?php
ob_start();
include_once('connect.php');
session_start();
?>
<html>
<head>
<title>Upload an image</title>
</head>
<body>
<form enctype="multipart/form-data" action="index.php" method="POST">
<input type="file" name="image">
<input type="submit" value="Upload">
</form>
<?php
//file properties
$file = $_FILES['image']['tmp_name'];
if(!isset($file)) {
echo 'Please select an image.';
}else{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size == false){
echo 'That is not an image';
}else{
if (!$insert = mysqli_query($con,"INSERT INTO uploading_image(name,image) VALUES('$image_name','$image')")){
echo 'Problem uploading image';
}else{
$lastid = mysqli_insert_id();
echo 'Image uploaded. <br>Your image:<br><img src="get.php? id='.$lastid.'">';
}
}
}
?>
</body>
</html>
$get.php:
<?php
include_once('connect.php');
$id = addslashes($_REQUEST['id']);
$image = mysqli_query($con,"SELECT * FROM uploading_image WHERE id='$id'");
$find_image = mysqli_query($row = mysqli_fetch_array($image));
$image_db = $row['image'];
header("Content-type: image/png");
echo $image_db;
?>
最佳答案
检查数据库中字段图像的数据类型。它应该是 blob 或 longblob。替换以下代码
$find_image = mysqli_query($row = mysqli_fetch_array($image));
有了这个。
$find_image = mysqli_query($row = mysqli_fetch_assoc($image));
关于php - 显示数据库 mysql、PHP 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34755582/