首页 > 语言 > PHP > 正文

php登录操作的文件上传管理系统

2024-09-04 11:45:01
字体:大 中 小
来源:转载
供稿:网友
  1. <?php 
  2. $admin_pw="admin";//管理密码 
  3. $uploaddir="upload";//上传目录 
  4. session_start(); 
  5. if($_GET['action']=="getcode") 
  6. { 
  7.   setcode(); 
  8.   exit(); 
  9. } 
  10. if($_POST['password']==$admin_pw && $_POST['yz']==$_SESSION['yzcode']) 
  11. { 
  12.   $_SESSION['logined']=$admin_pw; 
  13. } 
  14. if($_GET['action']=="logout") 
  15. { 
  16.   $_SESSION['logined']=""; 
  17.   header("location: ".$_SERVER['PHP_SELF']); 
  18.   exit(); 
  19. } 
  20. if($_SESSION['logined']!=$admin_pw) 
  21. { 
  22. ?> 
  23. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  24. <html xmlns="http://www.w3.org/1999/xhtml"> 
  25. <head> 
  26. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  27. <title>请登录</title> 
  28. </head> 
  29. <body> 
  30. <form action="" method="post"> 
  31. 输入密码:<input type="password" name="password" style="width:100px;" /><br />验证字符:<input type="text" style="width:40px;" name="yz" /><a href="#" onclick="document.tzm.src='?action=getcode';"><img src="?action=getcode" alt="验证码" id="tzm" name="tzm" /></a><br /><input type="submit" value="进入管理" /> 
  32. </form> 
  33. </body> 
  34. </html> 
  35. <?php 
  36. } 
  37. else 
  38. { 
  39. ?> 
  40. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  41. <html xmlns="http://www.w3.org/1999/xhtml"> 
  42. <head> 
  43. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  44. <title>文件上传</title> 
  45. </head> 
  46. <body> 
  47. <?php 
  48.   if($_POST['ac']=="upload") 
  49.   { 
  50.  $fileall=explode('.',$_FILES['file']['name']); 
  51.  $filetype=$fileall[count($fileall)-1]; 
  52.  $filename=$uploaddir."/".$_FILES['file']['name']."_".rand(1,999999999).".".$filetype; 
  53.  $fileexists=file_exists($filename); 
  54.  while($fileexists==true) 
  55.  { 
  56.   $filename=$uploaddir."/".$_FILES['file']['name']."_".rand(1,999999999).".".$filetype; 
  57.   $fileexists=file_exists($filename); 
  58.  } 
  59.  if(move_uploaded_file($_FILES["file"]["tmp_name"],$filename)) 
  60.  { 
  61.   $url="http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];  
  62.   echo "文件:".$filename." 上传成功!<br>文件地址:<input type=text style='width:350px;' value=".dirname($url)."/".$filename." /><a href=".dirname($url)."/".$filename." target="_blank">测试</a>"; 
  63.  } 
  64.  else 
  65.  { 
  66.   echo "文件".$filename."上传失败!"; 
  67.  } 
  68.   } 
  69. ?> 
  70. <form action="" method="post" enctype="multipart/form-data"> 
  71. 选择文件:<input type="file" name="file" id="file" width="100px" /><input type="hidden" name="ac" value="upload" /><input type="submit" value="上传" /> 
  72. </form> 
  73. <p><a href="?action=logout">退出登录</a></p> 
  74. </body> 
  75. </html> 
  76. <?php 
  77. } 
  78.  
  79.  
  80. function setcode() 
  81. { 
  82.   Header("Content-type: image/gif"); 
  83.   $border = 0; //是否要边框 1要:0不要 
  84.   $how = 4; //验证码位数 
  85.   $w = $how*15; //图片宽度 
  86.   $h = 20; //图片高度 
  87.   $fontsize = 5; //字体大小 
  88.   $alpha = "abcdefghijkmnopqrstuvwxyz"; //验证码内容1:字母 
  89.   $number = "0123456789"; //验证码内容2:数字 
  90.   $randcode = ""; //验证码字符串初始化 
  91.   srand((double)microtime()*1000000); //初始化随机数种子 
  92.   $im = ImageCreate($w, $h); //创建验证图片 
  93.   $bgcolor = ImageColorAllocate($im, 255, 255, 255); //设置背景颜色 
  94.   ImageFill($im, 0, 0, $bgcolor); //填充背景色 
  95.   if($border) 
  96.   { 
  97.     $black = ImageColorAllocate($im, 0, 0, 0); //设置边框颜色 
  98.     ImageRectangle($im, 0, 0, $w-1, $h-1, $black);//绘制边框 
  99.   } 
  100.   for($i=0; $i<$how; $i++) 
  101.   {    
  102.     $alpha_or_number = mt_rand(0, 1); //字母还是数字 
  103.     $str = $alpha_or_number ? $alpha : $number; 
  104.     $which = mt_rand(0, strlen($str)-1); //取哪个字符 
  105.     $code = substr($str, $which, 1); //取字符 
  106.     $j = !$i ? 4 : $j+15; //绘字符位置 
  107.     $color3 = ImageColorAllocate($im, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100)); //字符随即颜色 
  108.     ImageChar($im, $fontsize, $j, 3, $code, $color3); //绘字符 
  109.     $randcode .= $code; //逐位加入验证码字符串 
  110.   }//开源代码Vevb.com 
  111.   $_SESSION['yzcode'] = $randcode; 
  112.   Imagegif($im); 
  113.   ImageDestroy($im); 
  114. } 
  115. ?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表