栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

非常经典的PHP文件上传类分享

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

非常经典的PHP文件上传类分享

文件上传是项目开发中比较常见的功能,但文件上传的过程比较繁琐,只要是有文件上传的地方就需要编写这些复杂的代码。为了能在每次开发中降低功能的编写难度,也为了能节省开发时间,通常我们都会将这些反复使用的一段代码封装到一个类中。

$value){ 
 
      if (!in_array($key,get_class_vars(get_class($this)))){ 
 continue; 
} 
      $this->setOption($key, $value);  
     } 
  } 
  function uploadfile($field) { 
     $return=true; 
    if (!$this->CheckPath()) { 
      $this->errormsg=$this->geterrorNum(); 
      return false; 
    } 
    $name=$_FILES[$field]['name']; 
    $tmpname=$_FILES[$field]['tmp_name']; 
    $filesize=$_FILES[$field]['size']; 
    $error=$_FILES[$field]['error'];  
     if (is_array($name)) { 
      $errors=array(); 
for ($i=0;$igetFile($name[$i],$tmpname[$i],$filesize[$i],$errors[$i])) { 
   if (!$this->CheckSize() && !$this->CheckType()) { 
     $errors=$this->getErrorNum(); 
     return false; 
     }     
 }else{ 
     $errors=$this->getErrorNum(); 
     return false; 
 } 
 if (!$return) { 
   $this->getFile(); 
 } 
} 
if ($return) { 
 $fileNames=array(); 
 for ($i=0;$igetFile($name[$i], $tmpname[$i], $filesize[$i], $filesize[$i])) { 
     $this->SetFileName(); 
     if (!$this->MoveFile()) { 
$errors[]=$this->getErrorNum(); 
$return=false; 
     }else{ 
$fileNames[]=$this->getNewName(); 
     } 
   }  
 } 
 $this->newname=$fileNames; 
} 
 $this->errormsg=$errors; 
 return $return; 
 
     }else{  
      if($this->getFile($name,$tmpname,filesize,$error)){ 
 if(!$this->CheckSize()){ 
   return false; 
 } 
 if(!$this->CheckType()){ 
   return false; 
 } 
  $this->SetFileName(); 
 if ($this->MoveFile()) { 
     return true; 
 }  
      }else{ 
      return false; 
      } 
 
      if (!$return) { 
      $this->setOption('ErrorNum', 0); 
      $this->errormsg=$this->geterrorNum(); 
      } 
     return $return;  
   } 
  } 
   
  private function setOption($key,$value){ 
    $key=strtolower($key); 
    $this->$key=$value; 
  } 
   
  private function getFile($name,$tmpname,$filetype,$filesize,$error=0){  
     
    $this->setOption('TmpName', $tmpname); 
    $this->setOption('OrgiName', $name); 
    $arrstr=explode('.', $name); 
    $this->setOption('FileType', $arrstr[count($arrstr)-1]);      
    $this->setOption('FileSize', $filesize); 
    return true; 
  } 
   
  private function CheckPath(){ 
    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 Is_Http_Post(){ 
    if (!is_uploaded_file($this->tmpname)) { 
      $this->setOption('ErrorNum',-6);  
      return false; 
    }else{ 
      return true; 
    } 
  } 
   
  private function CheckSize(){ 
    if ($this->filesize>$this->maxsize) { 
      $this->setOption('ErrorNum', -2); 
      return false; 
    }else{ 
      return true; 
    } 
  } 
   
  private function CheckType(){ 
    if (in_array($this->filetype, $this->allowtype)) { 
      return true; 
    }else{ 
      $this->setOption('ErrorNum', -1); 
      return false; 
    } 
  } 
  private function SetFileName(){ 
    if ($this->israndname) { 
      $this->setOption('NewName', $this->RandName()); 
    }else{ 
      $this->setOption('NewName',$this->orginame); 
    }  
  } 
   
  public function getNewName() { 
    return $this->newname; 
  } 
  private function RandName(){ 
    $rule=date("YmdHis").rand(0, 999); 
    return $rule.'.'.$this->filetype; 
  } 
  private function MoveFile(){ 
    if ($this->errornum) { 
      $filepath=rtrim($this->filaepath,'/').'/'; 
      $filepath.=$this->newname; 
      if (@move_uploaded_file($this->tmpname,$filepath)) { 
 return true; 
}else{ 
 $this->errormsg=$this->setOption('ErrorNum',-3 ); 
} 
    }else{ 
      return false; 
    } 
  } 
   
   function getErrorNum() { 
    $erstr="上传文件{$this->orginame}出错"; 
    switch ($this->errornum) { 
      case 4: 
$erstr.="没有文件被上传"; 
 break; 
      case 3: 
$erstr.="文件只被部分上传"; 
 break; 
      case 2: 
$erstr.="上传文件超过了HTML表单MAX_FILE_SIZE指定的值"; 
 break; 
      case 1: 
$erstr.="上传文件超过了php.ini配置文件中upload_max_filesize的值"; 
 break; 
      case 0: 
$erstr="上传{$this->orginame}成功"; 
 break; 
      case -1: 
$erstr="未允许的类型"; 
 break; 
      case -2: 
$erstr.="文件过大,不能超过{$this->maxsize}个字节";  
 break; 
      case -3: 
$erstr.="上传失败"; 
 break; 
      case -4: 
$erstr="创建上传目录失败,请重新指定上传目录"; 
 break; 
      case -5: 
$erstr="未指定上传路径"; 
 break; 
      case -6: 
$erstr="非法操作"; 
 break;      
      default: 
$erstr.="未知错误"; 
  
    } 
    return $erstr; 
  } 
 } 
?> 

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/43289.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号