php设计超级好用的文件上传处理类一 (37),$val){ $key=strtolower($key); //查看用户参数中数组的下标是否和成员属性名相同 if(!in_array($key,get_class_vars(get_class($this)))){ continue; } $this->setoption($key, $val); } } private function geterror(){ $str=上传文件{$this->originname}时出错:; switch($this->errornum){ case 4: $str .= 没有文件被上传; break; case 3: $str .= 文件只被部分上传; break; case 2: $str .= 上传文件超过了html表单中max_file_size选项指定的值; break; case 1: $str .= 上传文件超过了php.ini 中upload_max_filesize选项的值; break; case -1: $str .= 末充许的类型; break; case -2: $str .= 文件过大,上传文件不能超过{$this->maxsize}个字节; break; case -3: $str .= 上传失败; break; case -4: $str .= 建立存放上传文件目录失败,请重新指定上传目录; break; case -5: $str .= 必须指定上传文件的路径; break; default: $str .= 末知错误; } return $str.'
'; } //用来检查文件上传路径 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; $isetfiles($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; $isetfiles($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; } }
http://www.bkjia.com/phpjc/1008857.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1008857.htmltecharticlephp设计超级好用的文件上传处理类一 (37), ? php class fileupload { private $filepath; // 指定上传文件保存的路径 private $allowtype=array( ' gif ' , '...