图像上传到数据库但不在php的页面上显示?

时间:2016-09-22 09:13:12

标签: php html mysql blob image-uploading

我在php和MySQL中有图像上传和查看代码。点击“imageUpload.php”页面中的“提交”按钮后,图像存储在数据库中。但不显示在“listImages.php”页面中。我不知道是什么问题。我看到“image not displaying when uploading in php”但对我来说它似乎是不同的解决方案。这是我的代码请看看我错了。

imageUpload.php:

<?php

 /* CREATE TABLE IF NOT EXISTS `output_images` 
  (
  `imageId` tinyint(3) NOT NULL AUTO_INCREMENT,
  `imageType` varchar(25) NOT NULL DEFAULT '',
  `imageData` mediumblob NOT NULL,
   PRIMARY KEY (`imageId`)
   ) */    

if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysqli_connect("localhost", "root", "");
mysqli_select_db ("test");
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);

$sql = "INSERT INTO output_images(imageType ,imageData)
VALUES('{$imageProperties['mime']}', '{$imgData}')";
$current_id = mysqli_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_error());
if(isset($current_id)) {
    header("Location: listImages.php");
}
}
}
?>
<HTML>
<HEAD>
<TITLE>Upload Image to MySQL BLOB</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />
</form>
</div>
</BODY>
</HTML>

listImages.php:

<?php
$conn = mysqli_connect("localhost", "root", "");
mysqli_select_db("test");
$sql = "SELECT imageId FROM output_images ORDER BY imageId DESC"; 
$result = mysqli_query($sql);
?>
<HTML>
<HEAD>
<TITLE>List BLOB Images</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<?php
while($row = mysqli_fetch_array($result)) {
?>
    <img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" /><br/>
<?php       
}
  mysqli_close($conn);
?>
</BODY>
</HTML>

imageView.php:

<?php
$conn = mysqli_connect("localhost", "root", "");
mysqli_select_db("test") or die(mysqli_error());
if(isset($_GET['image_id'])) {
    $sql = "SELECT imageType,imageData FROM output_images WHERE imageId=" . $_GET['image_id'];
    $result = mysqli_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysqli_error());
    $row = mysqli_fetch_array($result);
    header("Content-type: " . $row["imageType"]);
    echo $row["imageData"];
}
mysqli_close($conn);
?>

listimage.php

2 个答案:

答案 0 :(得分:1)

这会对你有帮助

<a href="imageView.php?image_id=<?php echo $row["imageId"]; ?>">
  <img src="<?php echo $row['imagedata']; ?>" alt="my picture" height="128" width="128" />
</a>

答案 1 :(得分:1)

它应该是     

    $conn=mysqli_connect("ur_servername_ex_localhost","ur_username","ur_password","ur_db");
    mysqli_query($conn, $sql);