这里是我上传的代码..但它不工作。我已经使用了的file_get_contents功能.. 上传图像如何使用php脚本将图像上传到数据库?
</head>
<body>
<form action="upload1.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"/>
<input type="submit" value="Upload image" />
</form>
<?php
//connect to the database
$con = mysql_connect("localhost","root", "");
if(!$con)
{
die('Could not connect to the database:' . mysql_error());
echo "ERROR IN CONNECTION";
}
mysql_select_db("imagedatabase", $con);
//file properties
echo $file = $_FILES['image']['tmp_name'];
echo '<br />';
if(!isset($file))
echo "Please select an image";
else
{
$image = file_get_contents($_FILES['image']['tmp_name']);
echo $image_name = addslashes($_FILES['image']['name']); echo '<br \>';
echo $image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size == FALSE)
echo "That's not an image";
else
{
$insert = mysql_query("INSERT INTO images (image) VALUES ($image)",$con);
if(!$insert)
echo "Problem uploding the image. Please check your database";
else
{
$last_id = mysql_insert_id();
echo "Image Uploaded. <p /> Your image: <p /><img src=display.php? id=$last_id>";
}
}
}
mysql_close($con);
?>
</body>
</html>
和检索/显示的代码放在这方式..
<?php
//connect to the database
mysql_connect("localhost","root", "") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());
//requesting image id
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM images WHERE id = $id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Conten-type: image/jpeg");
echo $image;
mysql_close($connect);
?>
我已经创建了一个名为“imagedatabase”数据库和表
数据库imagedatabase表为图像列里面是ID -autoincrement,名字 - VARCHAR,图像 - BLOB ..请帮助..即时通讯困惑 – SimonCode 2012-01-04 08:02:38
这究竟是什么不起作用?你有任何错误消息?更精确... – 2012-01-04 08:03:28
已回答上: http://stackoverflow.com/questions/1636877/how-can-i-store-and-retrieve-images-from-a-mysql-database -using-php – 2012-01-04 08:05:30