PHP-简单分页和文件上传类
2017-02-23 07:22
447 查看
以下是封装的面向对象用于数据库分页显示,文件上传的简单类,用于来理解PHP面向对象的思想,仅供参考学习。
page.class.php
upload.class.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."页 "; $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(); } } }
相关文章推荐
- php简单文件上传类
- php 简单文件图片上传类
- php简单文件上传类
- php之简单的文件管理(基本功能)
- php使用ftp远程上传文件类(解决主从文件同步问题的简单方法)
- php 简单生成csv文件
- 适用于初学者的简易PHP文件上传类
- PHP多文件上传(简单实用)
- php简单读取.vcf格式文件的方法示例
- php中如何实现分页以及文件上传
- 简单的php图片上传类_php
- php简单的上传类分享
- 通过PHP简单实例介绍文件上传
- php 文件上传程序(二款简单文件上传程序)
- php文件上传类
- php实现简单文件下载的方法
- php 倒序读取txt 文件中最后几行的内容,带分页
- php简单分页
- php 多文件上传类
- PHP----文件上传类