通过php插入图片到mysql数据库,死活就是插入不进去,mysql的版本是5.5.20,下面上代码:
(代码查过好多遍了,应该没错啊,求大神看看是不是其他的原因)
1、upload.php这个是主要的代码,上传图片入数据库的
<?php
include('./conn.php');
if ($_POST['submit']) {
if ($_FILES['image']['size']) {
$names = $_FILES['image']['name'];
$arr = explode('.', $names);
$name = $arr[0]; //图片名称
$date = date('Y-m-d H:i:s'); //上传日期
$fp = fopen($_FILES['image']['tmp_name'], 'rb');
$type = $_FILES['image']['type'];
if (!$fp) {
showInfo('读取图片失败!');
} else {
$image = addslashes(fread($fp, filesize($_FILES['image']['tmp_name'])));
if ($image) {
$q = "insert into image (name, pic, type, date) values ('$name','$image','$type','$date')";
$result = mysql_query($q);
if ($result) {
showInfo('上传成功!');
} else {
showInfo('上传失败!');
}
} else {
showInfo('请选择要上传的文件!');
}
}
} else {
showInfo('请选择要上传的文件!');
}
}
function showInfo($info)
{
echo "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
echo "<meta http-equiv='refresh' content='1;url=index.php'>";
echo "</head>";
echo "<body>" . $info . "……</body>";
echo "</html>";
}
?>
2、index.php 这个是主页,用来提交图片的
<?php
include('./conn.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<!--
* Created on 2012-10-20
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
-->
<head>
<meta http-equive="Content-Type" content=text/html charset=utf-8>
<title> </title>
</head>
<body>
<form method='post' action='./upload.php' enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="submit" value="上传" />
</form>
<!-----------显示图片--------------------->
<table>
<?php
$ret = mysql_query('select * from image order by id desc');
if ($ret) {
while ($row = mysql_fetch_array($ret)) {
?>
<tr>
<td style='width:170px;'>
<img src="image.php?id=<?php
echo $row[id];
?>" width="170" height="150" border="0">
<div style='text-align:center;'><?php
echo $row['name'];
?></div>
<?php
echo $row['date'];
?>
</td>
</tr>
<?php
}
}
?>
</table>
<!-----------/显示图片--------------------->
</body>
</html>3、数据库结构
--
-- 表的结构 `image`
--CREATE TABLE IF NOT EXISTS `image` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` varchar(100) CHARACTER SET utf8 NOT NULL,
`pic` blob NOT NULL,
`type` varchar(50) CHARACTER SET utf8 NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;--
-- 转存表中的数据 `image`
--
(代码查过好多遍了,应该没错啊,求大神看看是不是其他的原因)
1、upload.php这个是主要的代码,上传图片入数据库的
<?php
include('./conn.php');
if ($_POST['submit']) {
if ($_FILES['image']['size']) {
$names = $_FILES['image']['name'];
$arr = explode('.', $names);
$name = $arr[0]; //图片名称
$date = date('Y-m-d H:i:s'); //上传日期
$fp = fopen($_FILES['image']['tmp_name'], 'rb');
$type = $_FILES['image']['type'];
if (!$fp) {
showInfo('读取图片失败!');
} else {
$image = addslashes(fread($fp, filesize($_FILES['image']['tmp_name'])));
if ($image) {
$q = "insert into image (name, pic, type, date) values ('$name','$image','$type','$date')";
$result = mysql_query($q);
if ($result) {
showInfo('上传成功!');
} else {
showInfo('上传失败!');
}
} else {
showInfo('请选择要上传的文件!');
}
}
} else {
showInfo('请选择要上传的文件!');
}
}
function showInfo($info)
{
echo "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
echo "<meta http-equiv='refresh' content='1;url=index.php'>";
echo "</head>";
echo "<body>" . $info . "……</body>";
echo "</html>";
}
?>
2、index.php 这个是主页,用来提交图片的
<?php
include('./conn.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<!--
* Created on 2012-10-20
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
-->
<head>
<meta http-equive="Content-Type" content=text/html charset=utf-8>
<title> </title>
</head>
<body>
<form method='post' action='./upload.php' enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="submit" value="上传" />
</form>
<!-----------显示图片--------------------->
<table>
<?php
$ret = mysql_query('select * from image order by id desc');
if ($ret) {
while ($row = mysql_fetch_array($ret)) {
?>
<tr>
<td style='width:170px;'>
<img src="image.php?id=<?php
echo $row[id];
?>" width="170" height="150" border="0">
<div style='text-align:center;'><?php
echo $row['name'];
?></div>
<?php
echo $row['date'];
?>
</td>
</tr>
<?php
}
}
?>
</table>
<!-----------/显示图片--------------------->
</body>
</html>3、数据库结构
--
-- 表的结构 `image`
--CREATE TABLE IF NOT EXISTS `image` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` varchar(100) CHARACTER SET utf8 NOT NULL,
`pic` blob NOT NULL,
`type` varchar(50) CHARACTER SET utf8 NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;--
-- 转存表中的数据 `image`
--
是不是图片过大,Blob 最大 65K MediumBlob 最大 16M 可以试着改下类型吧