<?php/***文件上传类***/classUpload{//上传到哪个目录protected$path='./upload/';//准许的MIMEprotected$allowmime=['p_w_picpath/png','p_w_picpath/jpg','p_w_picpath/jpeg','p_w_picpath/pjpeg','p_w_picpath/bmp','p_w_picpath/wbmp','p_w_picpath/gif','p_w_picpath/x-png'];//准许的后缀protected$allowsubfix=['jpg','png','jpeg','gif','bmp'];//准许的大小protected$allowsize=2097152;//是否准许随机文件名protected$israndname=true;//是否准许日期目录protected$isdatepath=true;//文件的大小protected$size;//文件的原名protected$orgname;//文件的新名protected$newname;//文件的后缀protected$subfix;//文件的MIME类型protected$mime;//文件的新路径protected$newpath;//错误号protected$errorno;//错误信息protected$errorinfo;//临时文件protected$tmpfile;//前缀protected$prefix;//初使化成员属性,传进来的东西特别多,我们要求以数组的形式来传递//foreach循环,取出键名、值//将键名设置为成员属性名,将键值设为成员属性值//注意键名的合法性//errorInfoerrorinfopublicfunction__construct(Array$config=[]){foreach($configas$key=>$value){$this->setOption($key,$value);}}protectedfunctionsetOption($key,$value){$key=strtolower($key);$allPro=get_class_vars(get_class($this));if(array_key_exists($key,$allPro)){$this->$key=$value;}}//设置一个get方法专门获得新路径//设置一个get方法专门来获得错误信息//成员方法上传方法publicfunctionuploadFile($field){//检测路径用户是否定义过,如果没有定义失败if(!file_exists($this->path)){$this->setOption('errorNo',-1);returnfalse;}//目录权限检测if(!$this->checkPath()){$this->setOption('errorNo',-2);returnfalse;}//获得$_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)){returnfalse;}//检测MIME是否合法,检测后缀是否合法,检测文件大小是否超过了自定义的大小if(!$this->checkMime()||!$this->checkSubfix()||!$this->checkSize()){returnfalse;}//新名$this->newname=$this->getNewName();//新路径处理$this->newpath=$this->getNewPath();//是否是上传文件//如果是上传文件移动上传文件至指定的目录//如果成员returntruereturn$this->move();}protectedfunctionmove(){if(!is_uploaded_file($this->tmpfile)){$this->setOption('errorNo',-6);returnfalse;}if(move_uploaded_file($this->tmpfile,$this->newpath.$this->newname)){returntrue;}else{$this->setOption('errorNo',-7);returnfalse;}}protectedfunctiongetNewPath(){$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;}}protectedfunctiongetNewName(){if($this->israndname){return$this->prefix.uniqid().'.'.$this->subfix;}else{return$this->prefix.$this->orgname;}}protectedfunctioncheckSize(){if($this->size>$this->allowsize){$this->setOption('errorNo',-5);returnfalse;}else{returntrue;}}protectedfunctioncheckSubFix(){if(in_array($this->subfix,$this->allowsubfix)){returntrue;}else{$this->setOption('errorNo',-4);returnfalse;}}protectedfunctioncheckMime(){if(in_array($this->mime,$this->allowmime)){returntrue;}else{$this->setOption('errorNo',-3);returnfalse;}}protectedfunctionsetFiles($name,$size,$type,$tmpName,$error){//1234670(正常)if($error){$this->setOption('errorNo',$error);returnfalse;}$this->orgname=$name;$this->size=$size;$this->tmpfile=$tmpName;$this->mime=$type;//后缀没处理$info=pathinfo($name);$this->subfix=$info['extension'];returntrue;/*$arr=explode('.',$name);$this->subfix=array_pop($arr);$arr=explode('.',$name);$this->subfix=$arr[count($arr)-1];$pos=strrpos($name,'.');echosubstr($name,$pos+1);*/}protectedfunctioncheckPath(){//检测路径是否是目录,如果不存在创建if(!is_dir($this->path)){returnmkdir($this->path,0755,true);}//检测路径是否可写,如果不写写更改权限if(!is_writeable($this->path)||!is_readable($this->path)){returnchmod($this->path,0755);}returntrue;}protectedfunctiongetErrorInfo(){switch($this->errorno){case1:$str='上传的文件超过了php.ini中upload_max_filesize选项限制的值';break;case2:$str='上传文件的大小超过了HTML表单中MAX_FILE_SIZE选项指定的值';break;case3:$str='部份件被上传';break;case4:$str='没有文件被上传';break;case6:$str='找不到临时文件夹';break;case7:$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;}publicfunction__get($key){if(in_array($key,['newpath','newname','errorno','size'])){return$this->$key;}elseif($key=='errorinfo'){return$this->getErrorInfo();}}}//调用:$upload=newUpload();$result=$upload->uploadFile('f');var_dump($result);