首页 > 编程技术 > php

php上传图片代码(同时图片保存到数据库)

发布时间:2016-11-25 17:34

下面提供的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();
}

// 方法二:只保存文件名,
// 保存文件名时,文件在php.ini配置文件中设置的upload临时目录中,也就是upload_tmp_dir 参数中

if ($photo<>""){ 
 if (($photo_type== "image/pjpeg")or($photo_type == "image/gif")){
  // c:winnt emp 使php.ini配置文件中设置的upload文件的临时目录
  $photodir="c:winnt emp/";
  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();
}
// 方法一:在mysql中保存图片文件,
// 如果有图片文件,打开图片文件,将图片文件中的数据用函数
// addslashes处理,然后传递给变量$data,
// addslashes函数是给字符串加入斜线,使字符串能够顺利写入数据库中
// 这样变量$data 中保存的就是图片文件的数据了
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");

下面提供了四款php生成随机密码函数哦,方法简单实用是一款用户自定的加密函数,这样如果不知道你的加密算法是很难破解的。

方法一

 代码如下 复制代码

function generate_password( $length = 8 ) {
    // 密码字符集,可任意添加你需要的字符
    $chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';

    $password = '';
    for ( $i = 0; $i < $length; $i++ )
    {
        // 这里提供两种字符获取方式
        // 第一种是使用 substr 截取$chars中的任意一位字符;
        // 第二种是取字符数组 $chars 的任意元素
        // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }

    return $password;
}

方法二

动生成6位数字、字母 混合密码

 代码如下 复制代码
<?php
$str = "0123456789abcdefghijklmnopqrstuvwxyz";   //   输出字符集 
$n = 6;   //   输出串长度 
$len = strlen($str)-1;
for($j=0 ; $j<200 ; $j++){
for($i=0 ; $i<$n; $i++){
    $s .=  $str[rand(0,$len)]; 
}
echo $s . "<br/>";
$s = "";
}
?>
自动生成数字、字母、符号的密码
<?php
      $a = "12345678";
      $b = "abcdefghijklmnopqistuvwxyz";
      $s = substr(str_shuffle($a), 0, 2);
      $e = substr(str_shuffle($b), 0, 2);
      echo $s . substr(str_shuffle("!@#$%^&*"), 0, 2) . $e;
?>

方法三

 代码如下 复制代码

function create_password($pw_length = 8)
{
    $randpwd = '';
    for ($i = 0; $i < $pw_length; $i++)
    {
        $randpwd .= chr(mt_rand(33, 126));
    }
    return $randpwd;
}

// 调用该函数,传递长度参数$pw_length = 6
echo create_password(6);


方法三

 代码如下 复制代码

<?php
function getmicrotime()
{
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}
 
// 记录开始时间
$time_start = getmicrotime();
   
// 这里放要执行的php代码,如:
// echo create_password(6);
 
// 记录结束时间
$time_end = getmicrotime();
$time = $time_end - $time_start;

 // 输出运行总时间
echo "执行时间 $time seconds";
?>

<html>
<head>
<title>php教程写的网页计数器代码</title>
<head>
<body>

 代码如下 复制代码

<?php
$countfile = "counter.txt";
//定义计数器写入的文件是当前目录下的counter.txt文件中,然后我们应当测试该文件能否打开
if (($fp = fopen($countfile, "r+")) == false) {
 //用读写模式打开文件,若不能打开就退出
 printf ("open file %s failed!",$countfile);
 exit;
}
else
{
//如果文件能够正常打开,就读入文件中的数据,假设是1
$count = fread ($fp,10);
//读取10位数据
$count = $count + 1;
//count ++
fclose ($fp);
//关闭当前文件
$fp = fopen($countfile, "w+");
//以覆盖模式打开文件
fwrite ($fp,$count);
//写入加1后的新数据
fputs($fp,$fg);
//显示计数结果
// 数字显示
echo "<div align=center><font size=5>计数次数:$count</font><br>";

// 图形模式计数
$fp = fopen ($countfile, "r"); //以只读模式打开文件
$array_count = 1; //定义一个表示数组元素位置的变量,下面要用
while (! feof($fp)) {
$current_number = fgetc($fp);
$counter_array[$array_count] = $current_number;
$array_elements = count ($counter_array);
$array_count = $array_count + 1;
}
echo "<div align=center><font size=5>计数次数:";
for ($array_id = 1;$array_id < $array_elements; ++ $array_id) {
echo "<img" width=100% src=countimg/".$counter_array[$array_id].".gif>";
}
echo "</font>";

fclose ($fp);
//并关闭文件
}

?>

</body>
</html>

判断用户登陆主要分几个过程,第一步是用户登陆成功后把数据保存到session中,然后当用户访问需要登陆过的用户权限时就来判断session是否为空,如果不是就登录成功。下面来看个实例

 

<?php教程
session_start();

if(getconfig("chatroom_admin")==$_post["username"]&&getconfig("chatroom_adminpassword")==md5($_post["password"]))
{
 //登陆成功,注册session
 session_register("administrator");
 if(isset($_session["hack_num"]))
 {
  session_unregister("hack_num");
 }
 $_session["administrator"] = "yes";
 header("location:main.php");
 exit;

}
else
{
 if($_session["hack_num"]=="")
 {
  session_register("hack_num");
  $_session["hack_num"] = 1;
 }
 else
 {
  $_session["hack_num"] ++;

 }
 header("location:../notice.php?id=".admin_login_lost);
 exit;
}
?>

看到红色的没,那里就是用户登录成功把数据保存到session['hack_num']面。

<?php
//这个页面首先判断用户是否正确登录,如未登录,就转到登录页面。
session_start();
if((!$user_id)or(!$user_array[hack_num])or(!$user_array[hack_num])){
    header("location: login.php3");
}
?>

上面这段代码是用来判断session['hack_num']是否有值了,有就是登录成功了。

本站原创转载注明来源http://www.111cn.net/phper/php.html

这是一款利用递归写的php无限级分类代码,是一款树型的无限级分类实例,一的无限都是利用递归来实现的,也有更高级的写法,二叉树来实现无限分类了。
 代码如下 复制代码
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<head>
<style type="text/css教程">
<!--
.menu { color:#000000; font-size: 12px; cursor: hand;}
</style>
<script type="text/网页特效">
function showmenu(menuid)
 {
 if(menuid.style.display=="none")
 {
 menuid.style.display="";
 }
 else
 {
 menuid.style.display="none";
 }
 }
</script>
<title>电视树状目录</title>
</head>
<body>
<?php
 $globals["id"] =1;
 $jibie=1;
 $sql="select * from tvmenu where bid=0";
 $result=mysql教程_query($sql,$conn);
 
 if(mysql_num_rows($result)>0)
 treemenu($conn,$result,$jibie,$id);
 
 function treemenu($conn,$result,$jibie)  //进入函数循环
 {
 $numrows=mysql_num_rows($result);
 echo "<table cellpadding='0' cellspacing='0' border='0'>";
 for($rows=0;$rows<$numrows;$rows++)
 {
 $menu=mysql_fetch_array($result);
 $sql="select * from tvmenu where bid=$menu[id]";
 $result_sub=mysql_query($sql,$conn);
 echo "<tr>";
  //如果该菜单项目有子菜单,则添加javascript onclick语句
  if(mysql_num_rows($result_sub)>0)
  {
  echo "<td width='20'><img" width=100% src='+.gif' border='0'></td>";
  echo "<td class='menu' onclick='javascript:showmenu(menu".$globals["id"].");'>";
  }
  else
  {
  echo "<td width='20'><img" width=100% src='-.gif' border='0'></td>";
  echo "<td class='menu'>";
  }
  if($menu[url]!="")
  echo "<a href='$menu[url]'>$menu[name]</a> ";
  else
  echo "<a href='#'>$menu[name]</a>   <a href='?b={$menu[id]}&tpye=add'> 添加</a>  <a href='?b={$menu[id]}&tpye=edit'> 编辑</a>  <a href='?b={$menu[id]}&action=del'> 删除</a>";
  echo "</td> </tr>";
  if(mysql_num_rows($result_sub)>0)
  {
  echo "<tr id=menu".$globals["id"]++." style='display:none'>";
  echo "<td width='20'> </td>";
  echo "<td>";
  //将级数加1
  $jibie++;
  treemenu($conn,$result_sub,$jibie);
  $jibie--;
  echo "</td></tr>";
  }
  //显示下一菜单
  }
  echo "</table>";
}
?>
<br />
<br />
<?php if ($tpye=="add") { ?>
<table width="551" border="0" cellpadding="0" cellspacing="1" bgcolor="#cccccc">
<form action="" method="post" name="form1">
  <tr>
    <td height="28" colspan="2" align="center" bgcolor="#ffffff">添加栏目</td>
  </tr>
  <tr>
    <td width="246" height="28" align="right" bgcolor="#ffffff">栏目名称:</td>
    <td width="302" height="28" bgcolor="#ffffff"><input name="name" type="text" id="name" /></td>
  </tr>
  <tr>
    <td height="28" align="right" bgcolor="#ffffff">所属分类:</td>
    <td height="28" bgcolor="#ffffff"><select name="bid">
<?php
   
 $sql="select * from tvmenu";
 $que=mysql_query($sql,$conn);
    while($rs=mysql_fetch_array($que)){
  if ($rs['id']==$b) {
     $selected="selected="selected"";
     }else{
     $selected="";
     }
 ?>
 
      <option value="<?php echo $rs["id"]; ?>" <?php echo $selected;?>><?php echo $rs["name"]; ?></option>
  <?php }?>
    </select>
    </td>
  </tr>
  <tr>
    <td height="28" colspan="2" align="center" bgcolor="#ffffff"><input type="submit" name="submit" value="提 交" /></td>
  </tr>
  </form>
</table>
<?php }?>

 

标签:[!--infotagslink--]

您可能感兴趣的文章: