刚学php没多久,为了更好的练习及熟悉php,自己花了大半天写了个php上传类,在这里做个笔记,欢迎朋友们对这个类做修改及优化。
php code//up_class.phpinputfile = $inputfile; $this->retype = $retype; $this->upmaxsize = $maxsize; $this->allowfile = $upext; $this->upfolder = $upfolder; $this->isrename = $isrename; //$this->errorint = -1; } public function upfile(){ $_file_arr = $_files[$this->inputfile]; $this->errorint = $_file_arr['error']; if(is_uploaded_file($_file_arr['tmp_name'])){ if($_file_arr['tmp_name']){ $this->tmpname = $_file_arr['name']; $this->upmaxsize = $_file_arr['size']; $this->filetype = $_file_arr['type']; $this->tmppath = $_file_arr['tmp_name']; $this->filesize = $_file_arr['size']; if($this->upmaxsize > $this->upmaxsize){ $this->errorint = 6 ; //大小超出网站限制 } if(!$this->isallow()){ $this->errorint = 8 ; //系统不允许此类型文件 } if($this->isrename=='y'){ $this->savepath = $this->upfolder.$this->getfolder().'/'.$this->getnewname() ; $this->endfilename = $this->getnewname(); }else{ $this->savepath = $this->upfolder.$this->getfolder().'/'.$this->tmpname ; $this->endfilename = $this->tmpname; } //echo $this->errorint; if(!$this->errorint >= 1){ move_uploaded_file($this->tmppath,$this->savepath); } } } } public function getfileurl(){ switch($this->retype){ case 'n': return $this->endfilename; break; case 'pn': return $this->savepath; break; case 'js': return ; break; default: return $this->savepath; } } //获得新文件名 public function getnewname(){ return substr($this->tmpname,1,strrpos($this->tmpname,.)-1).'_'.mktime().'.'.$this->getfileext(); } public function upstatus(){ //echo $this->errorint; switch ($this->errorint){ case 1: return '超过了文件大小php.ini中限制大小'; break; case 2: return '超过了文件大小max_file_size 选项指定的值'; break; case 3: return '文件只有部分被上传'; break; case 4: return '没有文件被上传'; break; case 5: return '上传文件大小为0'; break; case 6: return '大小超出网站限制'; break; case 7: return '网站内没有指定这种上传类型'; break; case 8: return '系统不允许此类型文件'; case 9: return '创建目录失败!'; break; } } private function isallow(){ $allow = array( 0 => array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png','application/x-zip-compressed','application/octet-stream'), 1 => array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png'), 2 => array('application/x-zip-compressed','application/octet-stream'), 3 => array('','',''), 4 => array('','','') ); if($this->allowfile > count($allow)-1){ $this->errorint = 7; //网站内没有指定这种上传类型 }else{ if(in_array($this->filetype, $allow[$this->allowfile])){ return true; }else{ return false; } } } public function getfileext(){ //获得文件扩展名 return strtolower(substr($this->tmpname,strrpos($this->tmpname,.)+1)); } private function getfolder(){ //获得并自动创建相应文件夹 if(strpos('|rar|zip|7z|iso|','|'.$this->getfileext().'|')>=0){ $_folder = 'rar'; }elseif(strpos('|gif|jpeg|jpg|png|bmp|pjpeg|psd|','|'.$this->getfileext().'|')>=0){ $_folder = 'img'; }elseif(strpos('|rm|rmvb|avi|mp4|swf|flv|wmv|','|'.$this->getfileext().'|')>=0){ $_folder = 'vide'; }elseif(strpos('|doc|txt|xls|mdb||','|'.$this->getfileext().'|')>=0){ $_folder = 'doc'; }else{ $_folder = 'other'; } if(!file_exists($this->upfolder.$_folder)){ if(!mkdir($this->upfolder.$_folder)){ $this->errorint = 9; //创建目录失败 } } return $_folder; } }