您的位置:首页 > 其它

文件上传与下载学习笔记(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 ();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: