';}//用来检查文件上传路径private function checkfilepath(){if(empty($this->filepath)) {$this->setoption('errornum', -5);return false;}if(!file_exists($this->filepath) || !is_writable($this->filepath)){if(!@mkdir($this->filepath, 0755)){$this->setoption('errornum', -4);return false;}}return true;}//用来检查文件上传的大小private function checkfilesize() {if($this->filesize > $this->maxsize){$this->setoption('errornum', '-2');return false;}else{return true;}}//用于检查文件上传类型private function checkfiletype() {if(in_array(strtolower($this->filetype), $this->allowtype)) {return true;}else{$this->setoption('errornum', -1);return false;}}//设置上传后的文件名称private function setnewfilename(){if($this->israndname){$this->setoption('newfilename', $this->prorandname());} else {$this->setoption('newfilename', $this->originname);}} //设置随机文件名称private function prorandname(){$filename=date(ymdhis).rand(100,999);return $filename.'.'.$this->filetype;}private function setoption($key, $val){$this->$key=$val;}//用来上传一个文件function uploadfile($filefield){$return=true;//检查文件上传路径if(!$this->checkfilepath()){$this->errormess=$this->geterror();return false;}$name=$_files[$filefield]['name'];$tmp_name=$_files[$filefield]['tmp_name'];$size=$_files[$filefield]['size'];$error=$_files[$filefield]['error'];if(is_array($name)){$errors=array();for($i=0; $iif($this->setfiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){if(!$this->checkfilesize() || !$this->checkfiletype()){$errors[]=$this->geterror();$return=false;}}else{$error[]=$this->geterror();$return=false;}if(!$return)$this->setfiles();}if($return){$filenames=array();for($i=0; $iif($this->setfiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){$this->setnewfilename();if(!$this->copyfile()){$errors=$this->geterror();$return=false;}else{$filenames[]=$this->newfilename;}}}$this->newfilename=$filenames;}$this->errormess=$errors;return $return;} else {if($this->setfiles($name, $tmp_name, $size, $error)){if($this->checkfilesize() && $this->checkfiletype()){$this->setnewfilename();if($this->copyfile()){return true;}else{$return=false;}}else{$return=false;}}else{$return=false;}if(!$return)$this->errormess=$this->geterror();return $return;}}private function copyfile(){if(!$this->errornum){$filepath=rtrim($this->filepath, '/').'/';$filepath.=$this->newfilename;if(@move_uploaded_file($this->tmpfilename, $filepath)) {return true;}else{$this->setoption('errornum', -3);return false;}}else{return false;}}//设置和$_files有关的内容private function setfiles($name=, $tmp_name='', $size=0, $error=0){$this->setoption('errornum', $error);if($error){return false;}$this->setoption('originname', $name);$this->setoption('tmpfilename', $tmp_name);$arrstr=explode('.', $name);$this->setoption('filetype', strtolower($arrstr[count($arrstr)-1]));$this->setoption('filesize', $size);return true;}//用于获取上传后文件的文件名function getnewfilename(){return $this->newfilename;}//上传如果失败,则调用这个方法,就可以查看错误报告function geterrormsg() {return $this->errormess;}}
复制代码
装好, 上传文件, php
