$value) {
$this->setOption($key,$value);
}
}
protected function setOption($key,$value)
{
$key = strtolower($key);
$allPro = get_class_vars(get_class($this));
if (array_key_exists($key,$allPro)) {
$this->$key = $value;
}
}
//设置一个get方法专门获得新路径
//设置 一个get方法专门来获得错误信息
//成员方法上传方法
public function uploadFile($field)
{
//检测路径用户是否定义过,如果没有定义失败
if (!file_exists($this->path)) {
$this->setOption('errorNo',-1);
return false;
}
//目录权限检测
if (!$this->checkPath()) {
$this->setOption('errorNo',-2);
return false;
}
//获得$_FILES当中五个基本信息
$name = $_FILES[$field]['name'];
$size = $_FILES[$field]['size'];
$type = $_FILES[$field]['type'];
$tmpName = $_FILES[$field]['tmp_name'];
$error = $_FILES[$field]['error'];
//传入到一个成员方法里面进行设置
if (!$this->setFiles($name,$size,$type,$tmpName,$error)) {
return false;
}
//检测MIME是否合法,检测后缀是否合法,检测文件大小是否超过了自定义的大小
if (!$this->checkMime() || !$this->checkSubfix() || !$this->checkSize()) {
return false;
}
//新名
$this->newname = $this->getNewName();
//新路径处理
$this->newpath = $this->getNewPath();
//是否是上传文件
//如果是上传文件移动上传文件至指定的目录
//如果成员return true
return $this->move();
}
protected function move()
{
if (!is_uploaded_file($this->tmpfile)) {
$this->setOption('errorNo',-6);
return false;
}
if (move_uploaded_file($this->tmpfile,$this->newpath.$this->newname)) {
return true;
} else {
$this->setOption('errorNo',-7);
return false;
}
}
protected function getNewPath()
{
$this->path = rtrim($this->path,'/').'/';
if ($this->isdatepath) {
$newpath = $this->path.date('Y/m/d/');
if (!file_exists($newpath)) {
mkdir($newpath,0755,true);
}
return $newpath;
} else {
return $this->path;
}
}
protected function getNewName()
{
if ($this->israndname) {
return $this->prefix . uniqid() . '.' . $this->subfix;
} else {
return $this->prefix . $this->orgname;
}
}
protected function checkSize()
{
if ($this->size > $this->allowsize) {
$this->setOption('errorNo',-5);
return false;
} else {
return true;
}
}
protected function checkSubFix()
{
if (in_array($this->subfix,$this->allowsubfix)) {
return true;
} else {
$this->setOption('errorNo',-4);
return false;
}
}
protected function checkMime()
{
if (in_array($this->mime,$this->allowmime)) {
return true;
} else {
$this->setOption('errorNo',-3);
return false;
}
}
protected function setFiles($name,$size,$type,$tmpName,$error) {
//1 2 3 4 6 7 0(正常)
if ($error) {
$this->setOption('errorNo',$error);
return false;
}
$this->orgname = $name;
$this->size = $size;
$this->tmpfile = $tmpName;
$this->mime = $type;
//后缀没处理
$info = pathinfo($name);
$this->subfix = $info['extension'];
return true;
/*
$arr = explode('.',$name);
$this->subfix = array_pop($arr);
$arr = explode('.',$name);
$this->subfix = $arr[count($arr)-1];
$pos = strrpos($name,'.');
echo substr($name,$pos + 1);
*/
}
protected function checkPath()
{
//检测路径是否是目录,如果不存在创建
if (!is_dir($this->path)) {
return mkdir($this->path,0755,true);
}
//检测路径是否可写,如果不写写更改权限
if (!is_writeable($this->path) || !is_readable($this->path)) {
return chmod($this->path,0755);
}
return true;
}
protected function getErrorInfo()
{
switch ($this->errorno) {
case 1:
$str = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
break;
case 2:
$str = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
break;
case 3:
$str = '部份件被上传';
break;
case 4:
$str = '没有文件被上传';
break;
case 6:
$str = '找不到临时文件夹';
break;
case 7:
$str = '临时文件写入失败';
break;
case -1:
$str = '自定义的上传路径不存在';
break;
case -2:
$str = '没有权限';
break;
case -3:
case -4:
$str = '类型或后缀不准许';
break;
case -5:
$str = '超过了自定义的大小';
break;
case -6:
$str = '不是上传文件';
break;
case -7:
$str = '移动上传文件失败';
break;
}
return $str;
}
public function __get($key)
{
if (in_array($key, ['newpath','newname','errorno','size'])) {
return $this->$key;
} else if ($key == 'errorinfo') {
return $this->getErrorInfo();
}
}
}
//调用:
$upload = new Upload();
$result = $upload->uploadFile('f');
var_dump($result);
文章标题:PHP中面向对象的文件上传类
转载注明:
http://njwzjz.com/article/jseggh.html