我试图通过将图像保存在数据库[MySql]中来显示我的php页面中的图像,当我检索它时它没有显示我的图像。 但是,当我在MySql中单击它时,我可以看到图像,但它没有显示在php文件中。
图片表: id INT
的index.php
<html>
<head>
<title>Uploade an image</title>
</head>
<body>
<form action="test_image.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="Uploade">
</form>
<?php
mysql_connect("", "", "")
or die("<p>Error connecting to database: " . mysql_error() . "</p>");
mysql_select_db("test")
or die("<p>Error selecting the database: " . mysql_error() . "</p>");
// file propoerties
$file = $_FILES['image']['tmp_name'];
if(!isset($file))
echo "Please select an image.";
else
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size == FALSE)
echo "That's not an image";
else
{
if(!$insert = mysql_query("INSERT INTO Images (image) VALUES ('$image')"))
echo "Problem uploading image.";
else
{
$lastid = mysql_insert_id();
echo "Image uploaded <p /> Your image: <p /> <img width='500' height='500' src=getimage.php?id=$lastid>";
}
}
}
?>
</body>
</html>
getimage.php
<?php
mysql_connect("", "", "")
or die("<p>Error connecting to database: " . mysql_error() . "</p>");
mysql_select_db("test")
or die("<p>Error selecting the database: " . mysql_error() . "</p>");
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM Images WHERE id = $id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
我可以上传图片,但我无法检索它!
谢谢,
答案 0 :(得分:0)
你需要使用img html元素来查看图像
例如,这里是src保存图像文件的地方
<img src="images/yoursavedimagenameindatabase" width="" height="">
然后您可以在视图中查看已保存的图像
答案 1 :(得分:0)
$path="images/.$image['image']."
echo '<img width="100" height="66" src="'.$path.'" />';