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

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

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

  

上一篇:HTML页面中显示HTML标签</a><p><br>下一篇:<a href="/manong/747704.html">请求一个action,将图片的二进制字节字符串在视图页面以图片形式输出</a></p></div></article></div><section id="related-posts" class="widget-box"><h3>相关文章</h3><div class="widget-content"><ul><li class="other-news"><span>08-18</span><a href="/manong/1149370.html" title="【数据库】JDBC课设(5)将图片以二进制流方法添加进MySQL并查询"><i class="icon-angle-right"></i>【数据库】JDBC课设(5)将图片以二进制流方法添加进MySQL并查询</a></li><li class="other-news"><span>08-18</span><a href="/manong/747703.html" title="php将图片以二进制保存到mysql数据库并显示"><i class="icon-angle-right"></i>php将图片以二进制保存到mysql数据库并显示</a></li></ul><div class="clear"></div></div></section></div><aside class="span4 sidebar-right hide-sidebar"><div id="posts-list-widget-9" class="widget-box widget widget-posts"><div class="widget-title"><span class="icon"><i class="icon-list"></i></span><h3>编程语言最新文章</h3></div><div class="widget-content"><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/75990.html" title="Hbase问题汇总与解答">Hbase问题汇总与解答</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/526043.html" title="【两会速报】总理报告再提新一代人工智能,三大关键词全解读">【两会速报】总理报告再提新一代人工智能,三大关键词全解读</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/355319.html" title="Minikube - Kubernetes本地实验环境">Minikube - Kubernetes本地实验环境</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/340485.html" title="点亮1024获奖名单(更新至11.11)活动结束">点亮1024获奖名单(更新至11.11)活动结束</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/146831.html" title="AI 直播合集 | 五位阿里技术大咖共话人工智能的现在与未来">AI 直播合集 | 五位阿里技术大咖共话人工智能的现在与未来</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/10324.html" title="最新阿里云申请免费SSL证书实现网站HTTPS化(图文教程一)">最新阿里云申请免费SSL证书实现网站HTTPS化(图文教程一)</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/320301.html" title="独家下载!Java工程师成神必备宝典">独家下载!Java工程师成神必备宝典</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/387936.html" title="超全阿里系电子书年度合集来了!热门新书持续更新中...">超全阿里系电子书年度合集来了!热门新书持续更新中...</a></div></li><li ><div class="widget-thumb"><i class="icon-angle-right"></i><a href="/manong/354026.html" title="重磅下载!《2020前端工程师必读手册》,阿里巴巴前端委员会推荐!">重磅下载!《2020前端工程师必读手册》,阿里巴巴前端委员会推荐!</a></div></li><div class="clear"></div></div></div></aside></div></div></div><footer id="footer" class="row-fluid" role="contentinfo" itemscope=""><div class="span12 footer-nav"><ul><li><a href="/zuixin/">更多编程技术文章</a></li><li><a href="/sitemap.xml">网站地图</a></li></ul></div><div class="span12 footer-info"><p>本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮告之,本站会尽快删除。<span class="email-span"></span></p><p><a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">粤ICP备2022053706号</a></p></div></footer><div class="returnTop" title="" style="display: none;"><span class="s"></span><span class="b"></span>" </div><script type="text/javascript" src="/statics/553668/js/all.js"></script></body></html>