PHP-简单分页和文件上传类

以下是封装的面向对象用于数据库分页显示,文件上传的简单类,用于来理解PHP面向对象的思想,仅供参考学习。

page.class.php

<?php
//为分页效果封装一个类
class Page{
    public $total;
    public $show;
    public $pageNum;
    public $curpage;
    public $limitpage;
    public $spot = '#name'; //刷新锚点设置
    public function __construct($total,$show){
        $this->total = $total; //记录条数
        $this->show = $show; //显示数目,可更改
        $this->pageNum = ceil($this->total/$this->show); //总页数
        $this->curpage = empty($_GET['p'])?1:$_GET['p']; //当前页
        $this->limitpage = ($this->curpage-1)*$this->show; //开始位置
        if($this->curpage>$this->pageNum){
            $this->curpage = $this->pageNum;
        }
        if($this->curpage<0){
            $this->curpage == 1;
        }
    }
    public function showPage(){
      $pageData = "<li class='pbutton'>当前".$this->curpage."/".$this->pageNum."页&nbsp;&nbsp;";
      $pageData .= "共有".$this->total."条记录</li>";
      $pageData .= "<li class='pbutton'><a href='news.php?p=1".$this->spot."'>首页</a>";
      if($this->curpage>1){
        $pageData .= "<li class='pbutton'><a href='news.php?p=".($this->curpage-1).$this->spot."'>上一页</a>";
      }
      if($this->curpage<$this->pageNum){
        $pageData .= "<li class='pbutton'><a href='news.php?p=".($this->curpage+1).$this->spot."'>下一页</a>";
      }
      $pageData .= "<li class='pbutton'><a href='news.php?p=".$this->pageNum.$this->spot."'>尾页</a></li>";
      $pageData .= "<form method='get' action=''><input type='text' style='width:30px;height:20px;text-align:center;' name='p' value='$this->curpage'>
      <button type='submit'>跳转</button></form>";
      return $pageData;
    }
}

upload.class.php

<?php

//开始封装一个用来文件上传类,以功能为导向开始逻辑思考;
class Upload{
protected $fileName;    //上传的文件file名称  
protected $maxSize;        //预备的最大文件体积
protected $allowExt;   //允许的文件上传类型
protected $uploadPath; //预设的文件上传目录
protected $imgFlag;        //是否是真实的图片类型
protected $error;      //存放错误类型,用于调用报错
protected $ext;            //当前的文件扩展名
protected $uniName;        //产生唯一的字符串文件名
protected $destination;    //最后的上传文件地址,用于以后的数据库存储或调用
protected $fileInfo;    //上传的具体FILES信息

//构造函数,先初始化好需要的属性(变量);
public function __construct($fileName,$maxSize=8,$imgFlag=true,$allowExt=array('.jpeg','.jpg','.gif','.png'),$uploadPath='./uploads'){
    $this->fileName = $fileName;
    $this->maxSize = $maxSize*1024*1024;
    $this->allowExt = $allowExt;
    $this->imgFlag = $imgFlag;
    $this->uploadPath = $uploadPath;
    $this->fileInfo = $_FILES[$this->fileName];
}
//匹配错误信息
public function checkError(){
    if(!empty($this->fileInfo)){
        if($this->fileInfo['error']>0){
            switch($this->fileInfo['error']){
                    case 1:
                        $this->error = '上传文件超过了PHP配置文件中upload_max_filesize选项的值';
                        break;
                    case 2:
                        $this->error = '超过了表单MAX_FILE_SIZE限制的大小';
                        break;
                    case 3:
                        $this->error = '文件部分被上传';
                        break;
                    case 4:
                        $this->error = '没有选择上传文件';
                        break;
                    case 6:
                        $this->error = '没有找到临时目录';
                        break;
                    case 7:
                    case 8:
                        $this->error = '系统错误';
                        break;
                }
                return false;
            }else{
            return true;
            }
    }else{
        $this->error = '文件上传失败!';
        return false;
    }
}
//检测上传文件大小
public function checkSize(){
    if($this->fileInfo['size']>$this->maxSize){
            $this->error = '上传文件过大!';
            return false;
        }else{
            return true;
        }
}
//检测上传文件的扩展名
public function checkExt(){
    $this->ext = strrchr($this->fileInfo['name'],".");
    if(!in_array($this->ext, $this->allowExt)){
            $this->error = '不允许的文件类型!';
            return false;
    }else{
    return true;
    }
}
//检测是否是真实的图片类型
public function checkTrueImg(){
    if($this->imgFlag){
        if(!@getimagesize($this->fileInfo['tmp_name'])){
            $this->error = '不是真实的图片类型!';
            return false;
        }else{
        return true;
        }
    }
}
//显示错误
public function showError(){
    exit("<span style='color:#d00;'>".$this->error."</span>");
}
//检测目录是否存在
public function checkUploadPath(){
    if(!file_exists($this->uploadPath)){
        mkdir($this->uploadPath,0777,true);
        chmod($this->uploadPath,0777);
    }
}
//产生唯一字符串文件名
public function getuniName(){
    return md5(uniqid(microtime(true),true));
}
//开始上传文件
public function uploadFile(){
    if($this->checkError()&&$this->checkSize()&&$this->checkExt()&&$this->checkTrueImg()){
        $this->checkUploadPath();
        $this->uniName = $this->getuniName();
        $this->destination = $this->uploadPath."/".date('Y-m-d').$this->uniName.$this->ext;
        if(move_uploaded_file($this->fileInfo['tmp_name'], $this->destination)){
            return $this->destination;
        }else{
            $this->error = '文件移动失败!';
            $this->showError();
        }
    }else{
        $this->showError();
    }
}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值