php上传图片代码(同时图片保存到数据库),下面提供的php上传图片代码是一款利用php copy来上传文件的,他不但可以把图片上传到服务器,同时还把图片的地址保存到了mysql数据库里面。
-
- $conn = mysql_connect("localhost", "phpdb", "phpdb");
- mysql_select_db("test",$conn);
- ?>
- <?
-
- $id=$_post['id'];
-
- $checksql="select * from image where id='$id'";
- $check_re=mysql_query($checksql,$conn);
- $num=mysql_num_rows($check_re);
- if($num!=0){
- echo "<table width='100%' align=center><tr><td align=center>";
- echo "该用户名已经存在,请选择另一个<br>";
- echo "<font color=red>上传失败!</font><br><a href='网页特效:history.back(-1)'>返回</a>";
- echo "</td></tr></table>";
- exit();
- }
-
-
- if ($photo<>""){
- if (($photo_type== "image/pjpeg")or($photo_type == "image/gif")){
-
- $photodir="c:winnttemp/";
- if(!(file_exists($photo_name))){
-
- copy($photo,$photodir.$photo_name);
- }
- }
- else{
- echo "<script language=javascript>alert('上传照片只能是jpg或者gif!');history.go(-1)</script><br>";
- echo "或者<br>";
- echo "文件名已经存在,请为图片改一个文件名";
- exit;
- }
- }
- else{
- $photo_name="";
- }
- $sql="insert into image (id, photo) values('$id', '$photo_name')";
- mysql_query($sql,$conn) or die ("插入数据失败: ".mysql_error());
-
- mysql_close($conn);
-
-
- header("location:display_image2.php?id=$id");
- ?>
- 代码二
- <?php
-
- $conn = mysql_connect("localhost", "phpdb", "phpdb");
- mysql_select_db("test",$conn);
- ?>
- <?
-
- $id=$_post['id'];
-
- $checksql="select * from image where id='$id'";
- $check_re=mysql_query($checksql,$conn);
- $num=mysql_num_rows($check_re);
- if($num!=0){
- echo "<table width='100%' align=center><tr><td align=center>";
- echo "该用户名已经存在,请选择另一个<br>";
- echo "<font color=red>上传失败!</font><br><a href='javascript:history.back(-1)'>返回</a>";
- echo "</td></tr></table>";
- exit();
- }
-
-
-
-
-
- if ($photo<>""){
- $fp=fopen($photo,"r");
- $data=addslashes(fread($fp,filesize($photo)));
- }
- $password=md5($password);
- $sql="insert into image (id,photo) values('$id','$data')";
- mysql_query($sql,$conn) or die ("插入数据失败: ".mysql_error());
-
- mysql_close($conn);
-
-
- header("location:display_image1.php?id=$id");
|