文件上传与下载学习笔记(3)---面向对象方法实现文件上传
2015-04-10 11:43
661 查看
代码:
<?php class uploadClass { protected $filename; protected $maxSize; protected $allowExt; protected $allowMime; protected $uploadPath; protected $imgFlag; protected $fileInfo; protected $error; protected $ext; protected $uniName; protected $dst; //构造函数初始化 public function __construct($fileName = "myFile", $uploadPath = "oop", $maxSize = 5242880, $allowExt = array("jpeg","jpg","png","gif"), $allowMime = array("image/jpeg","image/jpg","image/png","image/gif"), $imgFlag = false) { $this->fileName = $fileName; $this->fileInfo = $_FILES [$this->fileName]; $this->maxSize = $maxSize; $this->allowMime = $allowMime; $this->ext = pathinfo ( $this->fileInfo ['name'], PATHINFO_EXTENSION ); $this->allowExt = $allowExt; $this->uploadPath = $uploadPath; $this->imgFlag = $imgFlag; $this->uniName=md5(uniqid(microtime(true),true)); $this->dst=$this->uploadPath.'/'.$this->uniName.'.'.$this->ext; } /** * 检测上传是否有错 * * @return boolean */ protected function checkError() { if ($this->fileInfo ['error'] == 0) { return true; } else { switch ($this->fileInfo ['error']) { case 1 : $this->error = $fileInfo ['name'] . '上传文件超过了upload_max_filesize 选项的值'; break; case 2 : $this->error = $fileInfo ['name'] . '超过了表单MAX_FILE_SIZE限制的大小'; break; case 3 : $this->error = $fileInfo ['name'] . '文件上传不完整'; break; case 4 : $this->error = $fileInfo ['name'] . '没有选择上传文件'; break; case 6 : $this->error = $fileInfo ['name'] . '没有找到临时目录'; break; case 7 : $this->error = $fileInfo ['name'] . '文件写入失败'; break; case 8 : $this->error = $fileInfo ['name'] . '文件上传被php扩展程序中断'; break; } return false; } } /** * 检查文件大小 * * @return boolean */ protected function checkSize() { if ($this->fileInfo ['size'] > $this->maxSize) { $this->error = "上传文件过大"; return false; } else { return true; } } /** * 检查文件类型 * * @return boolean */ protected function checkExt() { if (! in_array ( $this->ext, $this->allowExt )) { $this->error = "不正确的文件"; return false; } else { return true; } } /** * 检测文件的Mime类型 * * @return boolean */ protected function checkMime() { if (! in_array ( $this->fileInfo ['type'], $this->allowMime )) { $this->error = "文件类型不正确"; return false; } else { return true; } } /** * 检测是否是真实图片 * * @return boolean */ protected function checkTureImg() { if ($this->imgFlag) { if (@! getimagesize ( $this->fileInfo ['tmp_name'] )) { $this->error = "不是真正的图片"; return false; } else { return true; } }else{ return true; } } /** * 检测是否通过HTTP POST 上传 * @return boolean */ protected function checkPost() { if (!is_uploaded_file ( $this->fileInfo ['tmp_name'] )) { $this->error = "不是通过 HTTP POST 方式上传"; return false; } else { return true; } } /** * 显示错误信息 */ protected function showError(){ exit("<span color='red'>".$this->error."</span>"); } /** * 检测文件夹是否存在,不存在则创建 */ protected function checkPath(){ if(!file_exists($this->uploadPath)){ mkdir($this->uploadPath,0777,true); } } /** * 文件上传公共接口 */ public function uploadfile() { if ($this->checkError () && $this->checkSize () && $this->checkExt () && $this->checkMime () && $this->checkPost () && $this->checkTureImg()) { $this->checkPath(); if(move_uploaded_file($this->fileInfo['tmp_name'], $this->dst)){ return $this->dst; }else{ $this->error="文件移动失败"; $this->showError(); } } else { $this->showError (); } } }
相关文章推荐
- 文件上传与下载学习笔记(3)---面向对象方法实现文件上传
- 文件上传与下载学习笔记(3)---面向对象方法实现文件上传
- 云计算学习笔记004---hadoop的简介,以及安装,用命令实现对hdfs系统进行文件的上传下载
- Struts学习笔记: 实现文件上传下载
- 学习笔记2——远程连接linux服务器时SecureCRT的相关配置(包括实现对文件的上传、下载)
- 安装、进程-云计算学习笔记---hadoop的简介,以及安装,用命令实现对hdfs系统进行文件的上传下载-by小雨
- 文件上传下载学习笔记
- 文件上传与下载学习笔记(1)---文件上传原理及配置
- 黑马程序员_学习笔记22_HttpHanlder实现文件下载
- 【原创】Struts2学习笔记之文件的上传与下载
- Struts学习之 实现文件上传、下载操作
- struts2 学习笔记(三)——文件的上传和下载
- SilverLight学习笔记--使用WebClient实现通讯(一)(上传和下载字符串数据)
- php学习笔记(十)文件的上传与下载
- 学习笔记:基础+文件操作(上传|下载)+面向对象+xml
- cURL 学习笔记与总结(4)使用 cURL 从 ftp 上下载文件与上传文件到 ftp
- 文件的上传下载---struts2学习笔记
- 学习笔记之 文件的上传和下载代码
- [Struts2]struts2 实现文件上传【学习笔记】
- Struts2学习笔记15:Struts2的文件上传和下载【续】二