最近作了一個圖片上傳到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"> </td>
<td width="320" align="left">圖片名稱:
<input name="tpmc" type="text" id="tpmc" size="27" /></td>
</tr>
<tr>
<td height="33" align="center"> </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="上傳" />
<input name="qx" type="reset" id="qx" value="取消" /></td>
</tr>
<tr>
<td height="13" colspan="2" align="right"> </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"> </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"> 圖片名稱:<?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"> 圖片名稱:<?php echo $info[tpmc];?></td>
<td width="130">上傳時間: <?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%"> 頁次:<?php echo $page;?>/<?php echo $page_count;?>
頁 記錄:<?php echo $message_count;?>條 </td>
<td align="right" class="hongse01">
<?php
if($page!=1)
{
echo "<a href=index.php?page=1>首頁</a> ";
echo "<a href=index.php?page=".($page-1).">上一頁</a> ";
}
if($page<$page_count)
{
echo "<a href=index.php?page=".($page+1).">下一頁</a> ";
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(""," ",$tpmc); //將圖片名稱中的空格以「 "取代
$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(); ?>