php mysql 存储图片文件

最近作了一个图片上传到mysql中,可是出现了一些问题,就是图片能够上传,却不可以显示图片,请各位高手们帮帮忙: php

一、我建了一个数据库 html


二、index.php文件 mysql

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<table width="350" height="150" border="0" align="center" cellpadding="0" cellspacing="0" background=
"image/bg.gif">
<form name="form1" method="post" action="tptj_ok.php" enctype="multipart/form-data">
    <tr>
        <td height="46" colspan="2" align="right"><br /></td>
        </tr>
        <tr>
        <td width="30" height="27" align="center">&nbsp;</td>
            <td width="320" align="left">图片名称:
            <input name="tpmc" type="text" id="tpmc" size="27" /></td>
        </tr>
        <tr>
        <td height="33" align="center">&nbsp;</td>
            <td align="left">上传路径:<input name="filepath" type="file" size="23" maxlength="600" /></td>
        </tr>
        <tr>
        <td colspan="2" align="center"><input name="sc" type="submit" id="sc" value="上传" />&nbsp;
            <input name="qx" type="reset" id="qx" value="取消" /></td>
        </tr>
        <tr>
        <td height="13" colspan="2" align="right">&nbsp;</td>
        </tr>
    </form>
</table>
<!--显示图片-->
<table width="500" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#e5e5e5">
<tr align="left" colspan="2">
    <td height="25" colspan="3" background="images/top.gif">&nbsp;</td>
    </tr>
    <?php
include "../conn/conn.php";
$connection = mysql_connect($db_host, $db_username, $db_password);
if(!$connection)
{
die("Could not connect to the database: <br/>". mysql_error());
}
else
{
echo "Connect successful1!<br/>";
}
//select the database
$db_select=mysql_select_db($db_database);
if(!$db_select)
{
die("Could not select the database:<br/>". mysql_error());
}
else{
echo "select successful 1<br/>";
}
if($_GET[page]==""||is_numeric($_GET[page]==false)) //判断当前页的页数
{
$page=1;
}
else{
$page=$_GET[page];
}
$page_size=4; //每页显示4张图片
$query="select count(*) as total from tb_tpsc order by scsj desc";
$result=mysql_query($query); //查询总的记录条数
$message_count=mysql_result($result,0,"total"); //为变量赋值
if($message_count==0){
echo "暂无图片!";
}
else{
if($message_count<$page_size)
{
$page_count=1;
}
else{
if($message_count%$page_size==0)
{
$page_count=$message_count/$page_size;
}
else{
$page_count=ceil($message_count/$page_size);
}}
$offset=($page-1)*$page_size;
$query="select * from tb_tpsc where scsj order by id desc limit $offset,$page_size";
$result=mysql_query($query);
?>
        <table width="496" border="1" align="center" cellpadding="3" cellspacing="1" bordercolor=
        "#d6d7d6">
        <tr>
            <?php
$i=1;
while($info=mysql_fetch_array($result))
{if($i%2==0)
{?>
                <td width="500" height="180">
                      <table width="237" height="185" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td height="160" colspan="2">
                                <div align="center">
                  <img src="image.php?recid=<?php echo $info['id'];?>" width="225" height="160">
                                </div>
                            </td>
                        </tr>
                        <tr>
            <td width="105" height="25" align="left">&nbsp;图片名称:<?php echo $info[tpmc];?></td>
                            <td width="132">上传时间:<?php echo $info[scsj];?></td>
                        </tr></table>  </td>
               </tr>
               <?php  sql

}else{ 数据库

?> post

                <td width="500" height="180">
                <table width="236" height="185" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td height="160" colspan="2">
                                <div align="center">
                                <img src="image.php?recid=<?php echo $info[id];?>" width="225" height="160">
                                </div>
                            </td>
                        </tr>
                        <tr>
                        <td width="106" height="25">&nbsp;图片名称:<?php echo $info[tpmc];?></td>
                            <td width="130">上传时间:&nbsp;<?php echo $info[scsj];?></td>
                        </tr>
                    </table>
                </td>
                <?php
}
$i++;
}
   ?>
       </tr>
        </table>
        <table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
            <td width="52%">&nbsp;&nbsp;页次:<?php echo $page;?>/<?php echo $page_count;?>
                页 记录:<?php echo $message_count;?>条&nbsp;</td>
                <td align="right" class="hongse01">
                <?php
if($page!=1)
{
echo "<a href=index.php?page=1>首页</a>&nbsp;";
echo "<a href=index.php?page=".($page-1).">上一页</a>&nbsp;";
}
if($page<$page_count)
{
echo "<a href=index.php?page=".($page+1).">下一页</a>&nbsp;";
echo "<a href=index.php?page=".$page_count.">尾页</a>";
}
}
?>
  </td>
 </tr>
 </table> fetch

三、图片提交文件:tptj.php ui

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>


<body>
<?php
include("../conn/conn.php"); //连接数据源
$connection = mysql_connect($db_host, $db_username, $db_password);
if(!$connection)
{
die("Could not connect to the database: <br/>". mysql_error());
}
else
{
echo "Connect successful!<br/>";
}
//select the database
$db_select=mysql_select_db($db_database);
if(!$db_select)
{
die("Could not select the database:<br/>". mysql_error());
}
else
{
echo "select successful<br/>";
}
$tpmc = $_POST["tpmc"];
$file=$_FILES["filepath"];
$sc=$_POST["sc"];

if($sc!=="")
{

$tpmc=htmlspecialchars($tpmc); //将图片名称中的特殊字符转换成HTML格式
$tpmc=str_replace("\n","<br>",$tpmc); //将图片名称中的回车符以自动换行符取代
$tpmc=str_replace("","&nbsp;",$tpmc); //将图片名称中的空格以“&nbsp;"取代
$scsj=date("y;m;d"); //设置图片的上传时间
$fp=fopen($file['tmp_name'],"r"); //以只读方式打开文件
//if( $fp==
$file=addslashes(fread($fp,$file['size'])); //将文件中的引号部分加上反斜线
header("Content-Type:image/jpeg");
$query="insert into tb_tpsc(tpmc,file,scsj) values ('".$tpmc."','".$file."','".$scsj."')";//建立插入图片数据的SQL语句
$result= mysql_query($query);
//echo "<meta http-equiv=\"refresh\" content=\"1; url=index.php\">图片上传成功,请稍等...";

}
mysql_close();
?>
</body>
</html>
this

四、image.php url

<?php

header("Content-Type:image/jpeg"); include "../conn/conn.php"; $connection = mysql_connect($db_host, $db_username, $db_password); if(!$connection) { die("Could not connect to the database: <br/>". mysql_error()); } else { echo "Connect successful2!<br/>"; } //select the database $db_select=mysql_select_db($db_database); if(!$db_select) { die("Could not select the database:<br/>". mysql_error()); } else { echo "select successful2<br/>"; } $query="select * from tb_tpsc where id=".$recid; //查询指定ID的图片信息 $result=mysql_query($query); if(!$result) die("error:mysql query"); $num=mysql_num_rows($result); if($num<1) die("error:no this recorder"); //$data = mysql_result($result,0,'file'); //echo $data; //输出图片信息 $row=mysql_fetch_object($result); echo $row->file; mysql_close(); ?>

相关文章
相关标签/搜索