php将图片以二进制保存到mysql数据库并显示

时间:2023-03-08 16:01:16

一.存储图片的数据表结构:

--
-- 表的结构 `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`
--

  二.php将图片以二进制保存到mysql数据库中:

(1)连接数据库文件:conn.php:
<?php
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('study', $conn);
mysql_query("SET NAMES UTF-8");
?>

  (2)图片上传并以二进制保存到数据库文件: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>";
}
?>

  三.从数据库中读取以二进制保存的图片并显示:image.php

<?php
include('./conn.php'); $id = $_GET['id'];
$sql = "select * from image where id='$id'";
$result = mysql_query($sql, $conn);
if (!$result)
die("读取图片失败!");
$num = mysql_num_rows($result);
if ($num < 1)
die("暂无图片");
$data = mysql_result($result, 0, 'pic');
$type = mysql_result($result, 0, 'type');
mysql_close($id);
Header("Content-type: $type");
echo $data;
?>

  四.上传并显示图片的页面: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="data: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>