我使用以下方式在我的数据库上传图片:
new.php:
<form action="<?php echo base_url(); ?>index.php/hen/insert" method="post" enctype="multipart/form-data">
<input type="file" name="image1">
insert.php:
$conn = mysqli_connect("localhost","root","");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_select_db($conn,"hen" );
$image1=addslashes($_FILES['image1']['tmp_name']);
$image1=file_get_contents($image1);
$image1=base64_encode($image1);
$sq="INSERT INTO hen (image1) VALUES
('$image1')
直到这一刻,一切都很好。我使用longblob类型在数据库上成功上传了数据库上的图像。
但当我想在另一页上显示图像时使用:
mysqli_select_db($conn,"hen" );
$id =$_REQUEST['id'];
$U=$_REQUEST['U'];
if ($U=="Supervisor"){
// sending query
$s=mysqli_query($conn,"SELECT * FROM `hen` WHERE `hen`.`HenNumber` = '$id'");
echo "<table>";
while($row=mysqli_fetch_array($s)){
$data = base64_decode($row["image1"]);
$im = imagecreatefromstring(file_get_contents($data));
echo 'study ID: '.$row["0"];
echo " <tr>";
header("content-type :image/png");
echo "<img border=\"0\" src=\"".$im."\" width=\"102\" alt=\"Your Name\" height=\"91\">";
echo " </tr>";
}
header("content-type :image/jpg");
echo " </table>";
我只看到html代码而不是我的真实图片。