这是一个基础的网站后台类别管理系统,一般针对网站主页页面设置进行简单设计操作,有基本的增删改查和回显功能,主要知识点有对mysql数据库的使用,文件上传下载,排序问题,回调函数的遍历使用以及一些基本操作,适合新手对知识点进行总结实践.
页面展示:
1.主页面
2.类别列表显示页面
3.类别添加页面
4.新闻列表页面
5.新闻列表添加页面
项目目录展示:
数据库展示:
代码如下:
admin.php:
<frameset cols="10%,*" frameborder="1" framespacing="0" border="1">
<frame src="left.php" >
<frame src="main.php" name="main">
</frameset>
left.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加类别</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<ul class="type">
<li><a href="typelist.php" target="main">类别管理</a></li>
<li><a href="typeadd.php" target="main">添加分类</a></li>
</ul>
<ul class="type">
<li><a href="newlist.php" target="main">新闻列表</a></li>
<li><a href="newadd.php" target="main">添加新闻</a></li>
</ul>
</body>
</html>
main.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加类别</title>
</head>
<body>
<h1>欢迎进入后台管理</h1>
</body>
</html>
typelist.php:
<?php
header("Content-type:text/html;charset=utf-8");
include 'include/mysqli.php';
?>
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title></title>
<style>
table td{padding: 5px 3px;}
table tr{background: #33CCFF; height: 40px; line-height: 40px}
/*table tr:nth-child(2n){background: #f0f0f0;}*/
</style>
<script>
function delall(){
var arrid="";
obox=document.getElementsByName("box");
for(i=0;i<obox.length;i++){
if(obox[i].checked){
arrid+=obox[i].value+",";
}
}
alert(arrid);
window.location.href="typesave.php?action=delall&arrid="+arrid;
}
</script>
</head>
<body>
<table width="100%">
<tr><td><input type="checkbox">全选</td><td>编号</td><td>类别名称</td><td>排序</td></td><td>操作</td></tr>
<?php
function show($fid,$color,$i){
$i++;
$blank ="";
for($n=0;$n<$i;$n++){
$blank.="---";
}
$rand=array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
$color='#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
global $mysqli;
$sql="select *from type where fid=$fid order by orderid asc";
$result=$mysqli->query($sql);
while($row=$result->fetch_assoc()){
?>
<tr ><td><input type="checkbox" name="box" value="<?php echo $row['id']?>"></td><td><?php echo $row['id']?></td><td><?php echo $blank.$row['typename'].$blank?></td><td><?php echo $row['orderid']?></td><td><a href="typeupdate.php?fid=<?php echo $row['fid']?>&id=<?php echo $row['id']?>">修改</a>
<a href="typesave.php?action=del&id=<?php echo $row['id']?>">删除</a>
<a href="typeadd.php?id=<?php echo $row['id']?>">添加子类</a>
</td></tr>
<?php
// $rand=array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
// $color=$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
show($fid=$row['id'],$color,$i);
}
}
show(0,$color='#33CCFF',0);
?>
<table width="100%" >
<tr style="background: #ffffff">
<td width="10%"></td>
<td width="10%"></td>
<td width="50%"></td><td width="10%"></td>
<td><a href="javascript:delall()">全部删除</a><a href="typeadd.php"> 添加类别</a></td>
</tr>
</table>
</body>
</html>
typeadd.php:
<?php
include 'include/mysqli.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加类别</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<form method="post" action="typesave.php?action=add">
<ul class="typecontent">
<?php
?>
<li>父类名称<select name="fid">
<option value="0">根目录</option>
<?php
function show($fid,$i){
$i++;
$blank="";
for($n=0;$n<$i;$n++){
$blank.="---";
}
global $mysqli;
$sql="select *from type where fid=$fid order by orderid desc";
$result=$mysqli->query($sql);
$id=$_GET["id"];
while($row=$result->fetch_assoc()){
?>
<option <?php if($id==$row['id']){echo "selected";}?> value="<?php echo $row['id']?>"><?php echo $blank.$row['typename'].$blank?></option>
<?php
show($fid=$row['id'],$i);
}
?>
<?php }
show(0,0);
?>
</select>
</li>
<li>类别名称<input class="inp" type="text" name="typename"></li>
<li>排 序<input class="inp" type="text" name="orderid"></li>
<li>
<input class="btn" type="submit" name="dosub" value="添加"></li>
</ul>
</form>
</body>
</html>
newlist.php:
<?php include 'include/mysqli.php';
include 'include/page.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
table td{padding: 5px 3px;}
table tr{background: #dddddd; height: 40px; line-height: 40px}
.on{color:red}
</style>
</head>
<body>
<div><form method="post" action="">
<select name="typeid">
<option value="">请选择类别</option>
<?php
function show($fid=0,$i=0){
$i++;
$kongge='';
for($n=0;$n<$i;$n++){
$kongge.=" ";
}
global $mysqli;
$sql = "select * from type where fid=$fid order by orderid desc";
$result = $mysqli->query($sql);
while ($row = $result->fetch_assoc()) {
?>
<option value="<?php echo $row['id'] ?>"><?php echo $kongge.$row['typename'] ?></option>
<?php
show($row['id'],$i+2);
}
}
show();
?>
</select>
<input type="text" name="title">
<input type="submit" name="dosub">
</form>
</div>
<table width="100%">
<tr><td width="10%">全选:<input type="checkbox"></td>
<td width="10%">编号</td>
<td width="10%">类别名称</td>
<td width="10%">标题</td>
<td width="20%">图片</td>
<td width="30%">时间</td>
<td>操作</td></tr>
</table>
<table width="100%">
<?php
$id=isset($_POST['typeid'])?$_POST['typeid']:"";
$title=isset($_POST['title'])?$_POST['title']:"";
if(isset($id)&&!empty($id)){
$sql="select *from news where typeid=$id";
if(empty($title)){
$sql=$sql;
}else{
$sql.=" and title like '%$title%'";
}
}else{
$sql="select * from news";
if(empty($title)){
$sql=$sql;
}else{
$sql.=" where title like '$title'";
}
}
$p=isset($_GET["page"])?$_GET["page"]:1;
$page=new page($sql,3,$p);
$result=$page->getPage();
while($row=$result->fetch_assoc())
{
$picurl=$row["picurl"];
$posttime=date("y-m-d h:i:s",$row["posttime"]);
?>
<tr><td width="10%"><input type="checkbox"></td>
<td width="10%"><?php echo $row["id"]?></td>
<td width="10%">
<?php
$typeid=isset($row["typeid"])?$row["typeid"]:"";
if($typeid=="0"){
echo "没有分类";
}else{
$sql1="select *from type where id='$typeid'";
$result1=$mysqli->query($sql1);
$row1=$result1->fetch_assoc();
if(is_array($row1)){
echo $row1["typename"];
}else{
echo $row1["typename"];
}
}
?>
</td>
<td width="10%"><?php echo $row["title"]?></td>
<td width="20%">
<img src="<?php echo $picurl?>" height="50">
</td>
<td width="30%"><?php echo $posttime?></td>
<td>
<a href="newupdate.php?id=<?php echo $row['id']?>&typeid=<?php echo $row['typeid']?>"; >修改</a>
<a href="newsave.php?action=del&id=<?php echo $row['id']?>">删除</a>
</td>
</tr>
<?php
}?>
</table>
<?php
//$totalnum=$page->getTotalNum();
$page->getList();
?>
</body>
</html>
newadd.php:
<?php
include 'include/mysqli.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script src="plugin/kindeditor/kindeditor/kindeditor-all.js"></script>
<script src="plugin/calendar/calendar.js"></script>
</head>
<body>
<table width="100%" border="1" bordercolor="#dddddd" style="border-collapse: collapse">
<form action="newsave.php?action=add" method="post" enctype="multipart/form-data">
<tr><td width="100">分类:</td><td><select name="typeid">
<option value="0">请选择类别</option>
<?php
function show($fid,$i){
$i++;
$blank="";
for($n=0;$n<$i;$n++){
$blank.="---";
}
global $mysqli;
$sql = "select *from type where fid=$fid order by orderid";
$result = $mysqli->query($sql);
while ($row = $result->fetch_assoc()) {
?>
<option value="<?php echo $row['id'] ?>"><?php echo $blank.$row['typename'].$blank?></option>
<?php
show($row['id'],$i);
}
}
show(0,0);
?>
</select></td></tr>
<tr><td>标题:</td><td><input type="text" name="title"></td></tr>
<tr><td>图片:</td><td><input type="file" name="picurl"></td></tr>
<tr><td>来源:</td><td><input type="text" name="source"></td></tr>
<tr><td>摘要:</td><td><textarea rows="6" cols="100" name="description"></textarea></td></tr>
<tr><td>文章的内容:</td><td><textarea name="content" rows="15" cols="100" id="content"></textarea>
<script>
KindEditor.ready(function(K) {
window.editor = K.create('#content',{
afterBlur:function(){this.sync();}
})
});
</script>
</td></tr>
<tr>
<td>日期</td><td><input type="text" name="posttime" id="posttime" readonly="readonly">
<script type="text/javascript">
Calendar.setup({
inputField : "posttime",
ifFormat : "%Y-%m-%d %H:%M:%S",
showsTime : true,
timeFormat : "24"
});
</script>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="dosub" value="提交"></td>
</tr>
</form>
</table>
</body>
</html>
typesave.php:
<?php
header("Content-type:text/html;charset=utf-8");
include 'include/mysqli.php';
if($_GET["action"]=="add"){
$fid=$_POST['fid'];
$typename=$_POST["typename"];
$orderid=$_POST["orderid"];
if(empty($typename)){
echo "<script>alert('类别名称不能为空!')</script>";
return false;
}
$sql = "insert into type(typename,orderid,fid) values('$typename','$orderid','$fid')";
if ($mysqli->query($sql)) {
echo "<script>alert('类别添加成功')</script>";
echo "<script>window.location='typelist.php'</script>";
}
}elseif ($_GET["action"]=="update"){
$typename=$_POST["typename"];
$orderid=$_POST["orderid"];
$id=$_POST["id"];
if(empty($typename)){
echo "<script>alert('类别名称不能为空!')</script>";
return false;
}
$sql = "update type set typename='$typename',orderid='$orderid' where id='$id'";
if ($mysqli->query($sql)) {
echo "<script>alert('类别修改成功')</script>";
echo "<script>window.location='typelist.php'</script>";
}
}elseif ($_GET["action"]=="del"){
$id=$_GET['id'];
$sql = "delete from type where id=$id";
if ($mysqli->query($sql)) {
echo "<script>alert('类别删除成功')</script>";
echo "<script>window.location='typelist.php'</script>";
}
}elseif ($_GET["action"]=="delall"){
$arrid=$_GET["arrid"];
$arr=rtrim($arrid,",");
$sql="delete from type where id in ($arr)";
$result=$mysqli->query($sql);
if($result){
echo "<script>alert('类别删除成功!')</script>";
echo "<script>window.location.href='typelist.php'</script>";
}
}
typeupdate.php:
<?php
include 'include/mysqli.php';
$id= isset($_GET["id"])?$_GET["id"]:0;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改类别</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<form method="post" action="typesave.php?action=update">
<ul class="typecontent">
<?php
$id=isset($_GET['id'])?$_GET['id']:1;
$sql="select *from type where id='$id'";
$result=$mysqli->query($sql);
$row=$result->fetch_assoc();
?>
<li>父类名称<select name="fid">
<option value="0">根目录</option>
<?php
$fid=$_GET['fid'];
$sql="select *from type where id=$fid order by orderid desc";
$result=$mysqli->query($sql);
while($row=$result->fetch_assoc()){
?>
<option <?php if($row['id']==$fid){echo "selected";}?> value="<?php $row['id']?>"><?php echo $row['typename']?></option>
<?php
}
?>
</select></li>
<?php
$id=$_GET['id'];
$sql1="select *from type where id=$id order by orderid desc";
$result1=$mysqli->query($sql1);
while($row1=$result1->fetch_assoc()){
?>
<li>类别名称<input class="inp" value="<?php echo $row1['typename']?>" type="text" name="typename"></li>
<li>排 序<input value="<?php echo $row1['orderid']?>" class="inp" type="text" name="orderid"></li>
<li><input type="hidden" name="id" value="<?php echo $row1['id']?>">
<input class="btn" type="submit" name="dosub" value="修改"></li>
<?php
}
?>
</ul>
</form>
</body>
</html>
newsave.php:
<?php
header("Content-type:text/html;charset=utf-8");
include 'include/mysqli.php';
include 'include/fileupload.class.php';
$action = isset($_GET["action"])?$_GET["action"]:"";
$up=new FileUpload();
if($action=='add')
{
$title=$_POST["title"];
$typeid=$_POST["typeid"];
if($typeid=='')
{
die("请选择类别");
}
if($title=="")
{
die("请填写标题");
}
$source=$_POST["source"];
$content=$_POST["content"];
if($up->upload("picurl"))
{
$picurl=$up->getFileName();
}
else{
die($up->getErrorMsg());
}
$posttime= strtotime($_POST["posttime"]);
$sql="insert into news(typeid,title,source,picurl,content,posttime)values($typeid,'$title','$source','$picurl','$content',$posttime)";
global $mysqli;
if($mysqli->query($sql))
{
echo "<script>alert('添加成功');window.location.href='newlist.php'</script>";
}
else{
echo "<script>alert('添加失败');</script>";
}
}elseif($action=='update'){
$id=$_POST["id"];
$title=$_POST["title"];
$typeid=$_POST["typeid"];
if($typeid=='')
{
die("请选择类别");
}
if($title=="")
{
die("请填写标题");
}
$source=$_POST["source"];
$content=$_POST["content"];
if($up->upload("picurl"))
{
$picurl=$up->getFileName();
}
else{
die($up->getErrorMsg());
}
$posttime= strtotime($_POST["posttime"]);
$sql="update news set typeid='$typeid',title='$title',source='$source',picurl='$picurl',content='$content',posttime='$posttime' where id=$id";
global $mysqli;
if($mysqli->query($sql))
{
echo "<script>alert('修改成功');window.location.href='newlist.php'</script>";
}
else{
echo "<script>alert('修改失败');</script>";
}
}elseif($action="del"){
$id=$_GET['id'];
$sql="delete from news WHERE id=$id";
$result=$mysqli->query($sql);
if($result){
echo "<script>alert('删除成功!');window.location.href='newlist.php'</script>";
}else{
echo "<script>alert('删除失败!');</script>";
}
}
newupdate.php:
<?php
include 'include/mysqli.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script src="plugin/kindeditor/kindeditor/kindeditor-all.js"></script>
<script src="plugin/calendar/calendar.js"></script>
</head>
<body>
<?php
?>
<table width="100%" border="1" bordercolor="#dddddd" style="border-collapse: collapse">
<?php
$id=$_GET["id"];
$typeid=$_GET["typeid"];
?>
<form action="newsave.php?action=update" method="post" enctype="multipart/form-data">
<tr><td width="100">分类:</td><td><select name="typeid">
<option value="">请选择类别</option>
<?php
function show($fid,$i){
$i++;
$blank="";
for($n=0;$n<$i;$n++){
$blank.="---";
}
global $mysqli;
$sql = "select *from type where fid=$fid order by orderid";
$result = $mysqli->query($sql);
while ($row = $result->fetch_assoc()) {
global $typeid;
?>
<option <?php if($typeid==$row['id']){echo "selected";}?> value="<?php echo $row['id'] ?>"><?php echo $blank.$row['typename'].$blank?></option>
<?php
show($row['id'],$i);
}
}
show(0,0);
?>
</select></td></tr>
<?php
global $id;
$sql="select *from news where id=$id";
$result=$mysqli->query($sql);
while($row=$result->fetch_assoc()){
?>
<input type="hidden" name="id" value="<?php echo $row['id']?>">
<tr><td>标题:</td><td><input type="text" name="title" value="<?php echo $row['title']?>"></td></tr>
<tr><td>图片:</td><td><input type="file" name="picurl" value="<?php echo $row['picurl']?>"></td></tr>
<tr><td>来源:</td><td><input type="text" name="source" value="<?php echo $row['source']?>"></td></tr>
<tr><td>摘要:</td><td><textarea rows="6" cols="100" name="description" value="<?php echo $row['description']?>"></textarea></td></tr>
<tr><td>文章的内容:</td><td><textarea name="content" rows="15" cols="100" id="content" value="<?php echo $row['content']?>"></textarea>
<script>
KindEditor.ready(function(K) {
window.editor = K.create('#content',{
afterBlur:function(){this.sync();}
})
});
</script>
</td></tr>
<?php
$posttime=date("Y-m-d h:i:s",$row['posttime']);
?>
<tr><td>日期</td><td><input type="text" name="posttime" value="<?php echo $posttime?>" id="posttime" readonly="readonly">
<script type="text/javascript">
Calendar.setup({
inputField : "posttime",
ifFormat : "%Y-%m-%d %H:%M:%S",
showsTime : true,
timeFormat : "24"
});
</script>
</td></tr>
<?php
}
?>
<tr><td></td><td><input type="submit" name="dosub" value="修改"></td></tr>
</form>
</table>
</body>
</html>
include/mysqli.php:
<?php
include 'conn.inc.php';
$mysqli=new mysqli(HOST,USER,PWD,DBNAME);
if($mysqli->connect_errno){
die('数据库链接出错'.$mysqli->connect_error);
}
include/conn.inc.php:
<?php
define("HOST",'localhost');
define("USER",'root');
define("PWD",'root');
define("DBNAME",'onecms');
include/page.php:
<?php
class page{
private $page;//当前页数
private $pagenum;//每一页显示的数量
private $totalnum;//总记录数
private $totalpage;//总页数
private $sql;
function __construct($sql,$pagenum,$page) {
$this->sql=$sql;
$this->pagenum=$pagenum;
$this->page=$page;
}
//获取记录
function getPage(){
global $mysqli;
// 0,6 1
// 6,6 2
// 12,6 3
$result=$mysqli->query($this->sql);
$this->totalnum=$result->num_rows;
$this->totalpage= ceil($this->totalnum/$this->pagenum);//获取总页数 向上取整
if($this->page<=0)
{
$this->page=1;
}
$startnum = ($this->page-1)*$this->pagenum;
$this->sql.=" limit $startnum,$this->pagenum";
$result=$mysqli->query($this->sql);
return $result;
}
//获取总记录数
// function getTotalNum(){
// global $mysqli;
// $result=$mysqli->query($this->sql);
// return $result->num_rows;
// }
function getList(){
//当总记录数小于等于每页显示的记录
if($this->totalnum<=$this->pagenum)
{
echo "总记录数:$this->totalnum";
}
//当总记录数大于每页显示的记录
else{
$prev= $this->page-1;
//5页>=5页
//当前页面大于等于总页数
if($this->page>=$this->totalpage){
$next=$this->totalpage; //当前页面等于总页面数
}
//当前页面小于总页数1<2
else{
$next= $this->page+1;
}
echo "<a href='?page=1'>首页</a><a href='?page= $prev'>上一页</a>";
//如果总页数小于7页
if($this->totalpage<=7)
{ //如果当前页面等于循环中的$i 高亮显示
for($i=1;$i<=$this->totalpage;$i++)
{
if($this->page==$i)
{
echo "<a href='?page=$i' class='on'>$i</a> ";
}
else{
echo "<a href='?page=$i'>$i</a> ";
}
}
}
else{
if($this->page==1 || $this->page==2 || $this->page==3)
{
$b=1;
$m=7;
}
if($this->page>3)
{
$b= $this->page-3;
$m= $this->page+3;
}
if($this->page==$this->totalpage || $this->page==$this->totalpage-1 || $this->page==$this->totalpage-2)
{
$b=$this->totalpage-6;
$m= $this->totalpage;
}
if($this->page>4)
{
echo "...";
}
for($i=$b;$i<=$m;$i++){
if($this->page==$i)
{
echo "<a href='?page=$i' class='on'>$i</a> ";
}
else{
echo "<a href='?page=$i'>$i</a> ";
}
}
if($this->page<$this->totalpage-3)
{
echo "...";
}
}
echo "<a href='?page=$next'>下一页</a><a href='?page= $this->totalpage'>末页</a>";
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
include/page.class.php:
<?php
class page{
private $pagenum;
private $sql;
private $page;
private $totalnum;
function __construct($sql,$pagenum,$page) {
$this->sql=$sql;
$this->pagesize=$pagesize;
$this->page=$page;
}
function getpage(){
global $mysqli;
$startnum= ($this->page-1)*$this->pagenum;
$this->sql.=" limit $startnum,$this->pagenum";
$result=$mysqli->query($this->sql);
return $result;
}
function gettotalpage(){
global $mysqli;
$result=$mysqli->query($this->sql);
$this->totalnum= $result->num_rows;
}
function getlist(){
return ;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
include/fileupload.class.php:
<?php
/**
file: fileupload.class.php 文件上传类FileUpload
本类的实例对象用于处理上传文件,可以上传一个文件,也可同时处理多个文件上传
*/
class FileUpload {
private $path = "uploads"; //上传文件保存的路径
private $allowtype = array('jpg','gif','png'); //设置限制上传文件的类型
private $maxsize = 1000000; //限制文件上传大小(字节)
private $israndname = true; //设置是否随机重命名文件, false不随机
private $originName; //源文件名
private $tmpFileName; //临时文件名
private $fileType; //文件类型(文件后缀)
private $fileSize; //文件大小
private $newFileName; //新文件名
private $errorNum = 0; //错误号
private $errorMess=""; //错误报告消息
/**
* 用于设置成员属性($path, $allowtype,$maxsize, $israndname)
* 可以通过连贯操作一次设置多个属性值
*@param string $key 成员属性名(不区分大小写)
*@param mixed $val 为成员属性设置的值
*@return object 返回自己对象$this,可以用于连贯操作
*/
function set($key, $val){
$key = strtolower($key);
if( array_key_exists( $key, get_class_vars(get_class($this) ) ) ){
$this->setOption($key, $val);
}
return $this;
}
/**
* 调用该方法上传文件
* @param string $fileFile 上传文件的表单名称
* @return bool 如果上传成功返回数true
*/
function upload($fileField) {
$return = true;
/* 检查文件路径是滞合法 */
if( !$this->checkFilePath() ) {
$this->errorMess = $this->getError();
return false;
}
/* 将文件上传的信息取出赋给变量 */
$name = $_FILES[$fileField]['name'];
$tmp_name = $_FILES[$fileField]['tmp_name'];
$size = $_FILES[$fileField]['size'];
$error = $_FILES[$fileField]['error'];
/* 如果是多个文件上传则$file["name"]会是一个数组 */
if(is_Array($name)){
$errors=array();
/*多个文件上传则循环处理 , 这个循环只有检查上传文件的作用,并没有真正上传 */
for($i = 0; $i < count($name); $i++){
/*设置文件信息 */
if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i] )) {
if(!$this->checkFileSize() || !$this->checkFileType()){
$errors[] = $this->getError();
$return=false;
}
}else{
$errors[] = $this->getError();
$return=false;
}
/* 如果有问题,则重新初使化属性 */
if(!$return)
$this->setFiles();
}
if($return){
/* 存放所有上传后文件名的变量数组 */
$fileNames = array();
/* 如果上传的多个文件都是合法的,则通过销魂循环向服务器上传文件 */
for($i = 0; $i < count($name); $i++){
if($this->setFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i] )) {
$this->setNewFileName();
if(!$this->copyFile()){
$errors[] = $this->getError();
$return = false;
}
$fileNames[] = $this->newFileName;
}
}
$this->newFileName = $fileNames;
}
$this->errorMess = $errors;
return $return;
/*上传单个文件处理方法*/
} else {
/* 设置文件信息 */
if($this->setFiles($name,$tmp_name,$size,$error)) {
/* 上传之前先检查一下大小和类型 */
if($this->checkFileSize() && $this->checkFileType()){
/* 为上传文件设置新文件名 */
$this->setNewFileName();
/* 上传文件 返回0为成功, 小于0都为错误 */
if($this->copyFile()){
return true;
}else{
$return=false;
}
}else{
$return=false;
}
} else {
$return=false;
}
//如果$return为false, 则出错,将错误信息保存在属性errorMess中
if(!$return)
$this->errorMess=$this->getError();
return $return;
}
}
/**
* 获取上传后的文件名称
* @param void 没有参数
* @return string 上传后,新文件的名称, 如果是多文件上传返回数组
*/
public function getFileName(){
return $this->path.'/'.$this->newFileName;
}
/**
* 上传失败后,调用该方法则返回,上传出错信息
* @param void 没有参数
* @return string 返回上传文件出错的信息报告,如果是多文件上传返回数组
*/
public function getErrorMsg(){
return $this->errorMess;
}
/* 设置上传出错信息 */
private function getError() {
$str = "上传文件<font color='red'>{$this->originName}</font>时出错 : ";
switch ($this->errorNum) {
case 4: $str .= "没有文件被上传"; break;
case 3: $str .= "文件只有部分被上传"; break;
case 2: $str .= "上传文件的大小超过了HTML表单中MAX_FILE_SIZE选项指定的值"; break;
case 1: $str .= "上传的文件超过了php.ini中upload_max_filesize选项限制的值"; break;
case -1: $str .= "未允许类型"; break;
case -2: $str .= "文件过大,上传的文件不能超过{$this->maxsize}个字节"; break;
case -3: $str .= "上传失败"; break;
case -4: $str .= "建立存放上传文件目录失败,请重新指定上传目录"; break;
case -5: $str .= "必须指定上传文件的路径"; break;
default: $str .= "未知错误";
}
return $str.'<br>';
}
/* 设置和$_FILES有关的内容 */
private function setFiles($name="", $tmp_name="", $size=0, $error=0) {
$this->setOption('errorNum', $error);
if($error)
return false;
$this->setOption('originName', $name);
$this->setOption('tmpFileName',$tmp_name);
$aryStr = explode(".", $name);
$this->setOption('fileType', strtolower($aryStr[count($aryStr)-1]));
$this->setOption('fileSize', $size);
return true;
}
/* 为单个成员属性设置值 */
private function setOption($key, $val) {
$this->$key = $val;
}
/* 设置上传后的文件名称 */
private function setNewFileName() {
if ($this->israndname) {
$this->setOption('newFileName', $this->proRandName());
} else{
$this->setOption('newFileName', $this->originName);
}
}
/* 检查上传的文件是否是合法的类型 */
private function checkFileType() {
if (in_array(strtolower($this->fileType), $this->allowtype)) {
return true;
}else {
$this->setOption('errorNum', -1);
return false;
}
}
/* 检查上传的文件是否是允许的大小 */
private function checkFileSize() {
if ($this->fileSize > $this->maxsize) {
$this->setOption('errorNum', -2);
return false;
}else{
return true;
}
}
/* 检查是否有存放上传文件的目录 */
private function checkFilePath() {
if(empty($this->path)){
$this->setOption('errorNum', -5);
return false;
}
if (!file_exists($this->path) || !is_writable($this->path)) {
if (!@mkdir($this->path, 0755)) {
$this->setOption('errorNum', -4);
return false;
}
}
return true;
}
/* 设置随机文件名 */
private function proRandName() {
$fileName = date('YmdHis')."_".rand(100,999);
return $fileName.'.'.$this->fileType;
}
/* 复制上传文件到指定的位置 */
private function copyFile() {
if(!$this->errorNum) {
$path = rtrim($this->path, '/').'/';
$path .= $this->newFileName;
if (@move_uploaded_file($this->tmpFileName, $path)) {
return true;
}else{
$this->setOption('errorNum', -3);
return false;
}
} else {
return false;
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
插件用到一个日历编辑器:
calendar.js
一个文本编辑器:kindeditor-all.js
以上便是全部代码,
以上便是全部代码,
希望对和我一样的小白有点作用.