有关php向mysql上传图片
我用PHP,想在网页上上传一个图片,然后通过mysql存到网站的一个文件夹中(最好还能重新命名一下),然后,另一个网页再调用这个图片,显示出来。请教教我,这两个实现,最好...
我用PHP,想在网页上上传一个图片,然后通过mysql存到网站的一个文件夹中(最好还能重新命名一下),然后,另一个网页再调用这个图片,显示出来。请教教我,这两个实现,最好是整个代码,最好还能解释一下意思。。谢谢:)
还有一个问题,好心人也可以帮我看看,谢谢 :) 展开
还有一个问题,好心人也可以帮我看看,谢谢 :) 展开
2个回答
展开全部
1.php
<form action="2.php" method="post" enctype="multipart/form-data" name="f1">
<input type="file" name="myfile"/>
<input type="submit" name="submit"/>
</form>
2.php
<?php
session_start();
echo $_FILES['myfile']['name'];
$picture = 'upload/'.$_FILES['myfile']['name'];
move_uploaded_file($_FILES['myfile']['tmp_name'] , $picture);
echo $picture;
$_SESSION["href"]= $picture ;
$_SESSION["picture"] = $_FILES["myfile"]['name'];
?>
<br/>
<a href="3.php">下一步</a>
3.php
<?php
session_start();
?>
<form action="4.php" method="post" name="f1">
<input type="text" name="title"/>
<input type="submit" value="确定">
</form>
4.php
<?php
session_start();
$title = $_POST["title"];
$dater = date('Y-d-m');
$conn = mysql_connect('localhost' , 'root' , '316520');
if (!mysql_select_db('zzwsd' , $conn)){
echo "error";
}
$id = 0;
$sql = "select * from zzwsd" ;
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)){
$id = $row[0];
$id ++ ;
}
$sql = "INSERT INTO zzwsd (id , title , picture , href , dater) VALUES ('$id' , '$title' , '$_SESSION[picture]' , '$_SESSION[href]' , '$dater')" ;
$query = mysql_query($sql);
echo "导入成功<br/>" ;
echo "<a href=zzwsd.php>前台展示</a><br/>";
$sql = "select * from zzwsd" ;
$query = mysql_query ($sql);
$i = 0;
while ($row = mysql_fetch_array($query)){
$i ++ ;
echo "第".$i."条记录: ".$row[0]." ".$row[1]." ".$row[2]." ".$row[3]." ".$row[4]." "."<br/><br/>";
}
?>
del.php
<?php
echo "这是删除页面 ".$_GET[id];
?>
mod.php
<?php
echo "这修改页面 ".$_GET[id];
?>
zzwsd.php
<?php
$cho = 101;
echo "这里是前台";
?>
<br/><br/>
<div style="margin-left:50px;width:1000px;background:#CCCCFF">
<?php
$conn = mysql_connect('localhost' , 'root' , '316520');
if(!mysql_select_db('zzwsd' , $conn)){
echo "链接数据库失败";
}
$i = 0 ;
$sql = "select * from zzwsd limit $i , 30";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)){
?>
<div style="margin-left:10px;margin-top:20px;width:220px; height:200px; float:left">
<img src=<?php echo $row[3]?> width=160px height=200px style="float:left"/>
<div style="float:left;width:60px">
<?php if($cho == 101){ ?>
<br/>
<a href=<?php echo "del.php?id=".$row[0] ?> target="_self">删除</a>
<br/>
<br/>
<a href=<?php echo "mod.php?id=".$row[0] ?> target="_self">修改</a>
<?php }else{ ?>
<?php } ?>
</div>
</div>
<?php
}
?>
</div>
这是我当初学的时候练手的。拿去看看吧, 用mysql自己创建数据库和里面的字段就可以了,注意别乱码了
<form action="2.php" method="post" enctype="multipart/form-data" name="f1">
<input type="file" name="myfile"/>
<input type="submit" name="submit"/>
</form>
2.php
<?php
session_start();
echo $_FILES['myfile']['name'];
$picture = 'upload/'.$_FILES['myfile']['name'];
move_uploaded_file($_FILES['myfile']['tmp_name'] , $picture);
echo $picture;
$_SESSION["href"]= $picture ;
$_SESSION["picture"] = $_FILES["myfile"]['name'];
?>
<br/>
<a href="3.php">下一步</a>
3.php
<?php
session_start();
?>
<form action="4.php" method="post" name="f1">
<input type="text" name="title"/>
<input type="submit" value="确定">
</form>
4.php
<?php
session_start();
$title = $_POST["title"];
$dater = date('Y-d-m');
$conn = mysql_connect('localhost' , 'root' , '316520');
if (!mysql_select_db('zzwsd' , $conn)){
echo "error";
}
$id = 0;
$sql = "select * from zzwsd" ;
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)){
$id = $row[0];
$id ++ ;
}
$sql = "INSERT INTO zzwsd (id , title , picture , href , dater) VALUES ('$id' , '$title' , '$_SESSION[picture]' , '$_SESSION[href]' , '$dater')" ;
$query = mysql_query($sql);
echo "导入成功<br/>" ;
echo "<a href=zzwsd.php>前台展示</a><br/>";
$sql = "select * from zzwsd" ;
$query = mysql_query ($sql);
$i = 0;
while ($row = mysql_fetch_array($query)){
$i ++ ;
echo "第".$i."条记录: ".$row[0]." ".$row[1]." ".$row[2]." ".$row[3]." ".$row[4]." "."<br/><br/>";
}
?>
del.php
<?php
echo "这是删除页面 ".$_GET[id];
?>
mod.php
<?php
echo "这修改页面 ".$_GET[id];
?>
zzwsd.php
<?php
$cho = 101;
echo "这里是前台";
?>
<br/><br/>
<div style="margin-left:50px;width:1000px;background:#CCCCFF">
<?php
$conn = mysql_connect('localhost' , 'root' , '316520');
if(!mysql_select_db('zzwsd' , $conn)){
echo "链接数据库失败";
}
$i = 0 ;
$sql = "select * from zzwsd limit $i , 30";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)){
?>
<div style="margin-left:10px;margin-top:20px;width:220px; height:200px; float:left">
<img src=<?php echo $row[3]?> width=160px height=200px style="float:left"/>
<div style="float:left;width:60px">
<?php if($cho == 101){ ?>
<br/>
<a href=<?php echo "del.php?id=".$row[0] ?> target="_self">删除</a>
<br/>
<br/>
<a href=<?php echo "mod.php?id=".$row[0] ?> target="_self">修改</a>
<?php }else{ ?>
<?php } ?>
</div>
</div>
<?php
}
?>
</div>
这是我当初学的时候练手的。拿去看看吧, 用mysql自己创建数据库和里面的字段就可以了,注意别乱码了
展开全部
在上传代码中首先获取上传文件图片的文件名$filename,然后insert的时候将获取的文件名insert到adminphoto中
mysql_query("insert into admindata (adminphoto) values ('images/".$filename."')")
显示的时候
<img src="<?=$AdminPhoto?>">" width="111" height="144">
$AdminPhoto为查询到你数据库的图片路径
$AdminPhoto替换成$rs->AdminPhoto
mysql_query("insert into admindata (adminphoto) values ('images/".$filename."')")
显示的时候
<img src="<?=$AdminPhoto?>">" width="111" height="144">
$AdminPhoto为查询到你数据库的图片路径
$AdminPhoto替换成$rs->AdminPhoto
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询