无法显示mysql里的图片

chaiyanlin 2010-04-04 08:27:13
一下是我的代码:
upf1.html
<html>
<head>
<title>上传图片</title>
</head>
<body>
<form method="post" action="upf2.php" enctype="multipart/form-data"><center><br><br><br><br>
<input type="hidden" value="204800" name="MAX_FILE_SIZE"/>
File: <input type="file" name="imgfile" /><br><br>
<input type="submit" value="OK" name="submitbtn" style="width:100px;height:23px"/></center>
</form>
</body>
</html>

upf2.php
<?php
//向数据库中插入图片
$imgfile=$_FILES['imgfile'];
$submitbtn=$_POST['submitbtn'];
if($submitbtn=='OK' && is_array($imgfile))
{
$name=$imgfile['name']; //取得图片名称
$type=$imgfile['type']; //取得图片类型
$size=$imgfile['size']; //取得图片长度
$tmpfile=$imgfile['tmp_name']; //图片上传上来到临时文件的路径
if($tmpfile && is_uploaded_file($tmpfile)) //判断上传文件是否为空,文件是不是上传的文件
{
//读取图片流
$file=fopen($tmpfile,"rb");
$imgdata=bin2hex(fread($file,$size)); //bin2hex()将二进制数据转换成十六进制表示
fclose($file);

$con=mysql_connect("localhost","root","199026"); //连接数据库函数
mysql_select_db("andy"); //选择数据库
//插入出数据库语句,图片数据前要加上0x,用于表示16进制数
if (mysql_query("insert into images(image) values('0x.$imgdata')") )
echo "<center>insert successful!<br><br><a href='showimage.php'>show image</a></center>";
else
echo "<center>insert failed!</center>";
mysql_close();
}
else
echo "<center>Choose image!<br><br><a href='upf1.html'>back</a></center>";
}
else
echo "<center>choose image!<br><br><a href='upf1.html'>back/a></center>";
?>

showimage.php
<?php
mysql_connect("localhost","root","199026");
mysql_select_db("test");
//显示最新插入的那张图片
$result=mysql_query("select image from images ");
$row=mysql_fetch_object($result);
header("Content-Type:image/pjpeg");
echo $row->image;
mysql_close();
?>
显示图片是报错,错误在showimage.php里
提示错误是:
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\test\showimage.php on line 6

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\test\showimage.php:6) in C:\AppServ\www\test\showimage.php on line 7

菜鸟请求更正!
...全文
125 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
餅餅 2010-04-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 chaiyanlin 的回复:]
我晕死,我把db的名字写错了
现在可以输出,但输出的是16进制数!
怎么显示图呢!
[/Quote]
<img src='showimage.php' />
chaiyanlin 2010-04-04
  • 打赏
  • 举报
回复
我晕死,我把db的名字写错了
现在可以输出,但输出的是16进制数!
怎么显示图呢!
骄傲青蛙 2010-04-04
  • 打赏
  • 举报
回复
while($row=mysql_fetch_assoc($result)){
header("Content-Type:image/pjpeg");
echo $row->image;
}

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧