PHP头条
热点:

PHP中的一个很好用的文件上传类


<?php
 
 class FileUpload{
 
  private $filepath; //设置上传文件的路径
  private $allowtype=array('jpg','jpeg','gif','bmp'); //默认的文件的类型
  private $maxsize=2000000;   //默认文件的大小
  private $israndname=true; //是否使用随机文件名
  private $originName;  //源文件名称
  private $tmpFileName;  //临时文件名
  private $fileType;    //文件类型
  private $fileSize;   //文件的大小
  private $newFileName;  //新文件的名称
  private $errorNum;  //错误号
  private $errorMess=""; //用来提示错误报告
  //用于文件初始化的函数
  function __construct($options=array()){
   foreach($options as $key=>$val){
    $key=strtolower($key);//设置属性名称均为小写
    //get_class_vars(get_class($this))
    //获取当前类的所有的属性
    if(!in_array($key,get_class_vars(get_class($this)))){
     continue;
    }
    else
    {
     $this->setOption($key,$val);
    }
   }
  }

  //定义不同的错误级别
  private function getError(){
   $str="<font color='red'>上传文件{$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."</font><br/>";
  }

  //为成员属性赋值的函数
  private function setOption($key,$val){
   $this->$key=$val;
  }
  //用于检查上传文件的路径
  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;
   }
  }
  //上传一个文件
  function uploadFile($fileField){
   $return=true;
   $name=$_FILES[$fileField]["name"]; //post提交的名称
   $tmp_name=$_FILES[$fileField]["tmp_name"];  //上传时的临时文件名
   $size=$_FILES[$fileField]["size"];
   $error=$_FILES[$fileField]["error"];
   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(!$this->checkFilePath()){
    $this->errorMess=$this->getError();
    return false;
   }

   if(!$return)
   {
    $this->errorMess=$this->getError();
   }
   return $return;
  }
  //设置和$_FILES有关的内容
  private function setFiles($name="",$tmp_name="",$size=0,$error=0){
   $this->setOption("errorNum",$error);
   if($this->errorNum){
    return false;
   }
   $arrstr=explode(".",$name);
   $this->setOption("fileType",strtolower($arrstr[count($arrstr)-1]));
   $this->setOption("originName",$name);
   $this->setOption("tmpFileName",$tmp_name);
   $this->setOption("fileSize",$size);
   return true;
  }

  //用于获取上传文件后文件的名称
  function getNewFileName(){
   return $this->newFileName.'上传成功!';
  }


  //上传文件失败时,显示错误信息的函数
  function getErrorMsg(){
   return $this->errorMess;
  }
  private function proRandName(){
   $filename=date("YmdHis").rand(100,999);
   return $filename.".".$this->fileType;
  }

  //设置上传后的文件名称
  private function setNewFileName(){
   if($this->israndname){
    $this->setOption('newFileName',$this->proRandName());
   }
   else
   {
    $this->setOption('newFileName',$this->originName);
   }
  }

  //拷贝文件
  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;
    }
   }
  }
 
 
 }

?>


 

www.phpzy.comtrue/phprm/4013.htmlTechArticlePHP中的一个很好用的文件上传类 ?php class FileUpload{ private $filepath; //设置上传文件的路径 private $allowtype=array(jpg,jpeg,gif,bmp); //默认的文件的类型 private $maxsize=2000000; //默认文件的大小 priv...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐