php 간단 한 업로드 클래스 공유

10699 단어 php업로드 클래스
본 논문 의 사례 는 phop 업로드 류 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.

<?php
class UploadFile{
  var $inputName;         //   
  var $allowType = array(
        'image/gif','image/jpg','image/jpeg','image/png','image/x-png','image/pjpeg'
  );                //    
  var $allowSize = 2097152;  //    
  var $saveDir = UPLOAD;   //    
  var $isRename = true;        //     ,   true
  var $errID = 0;           //    ,   0
  var $errMsg = "";          //    
  var $savePath = "";         //    

  function __construct($inputName,$allowType="",$allowSize="",$saveDir="",$isRename=true){
    if(empty($inputName)){
      $this->chk_err(-1);    //      
    }else{
      $this->inputName = $inputName;
    }

    if(!empty($allowType)) $this->allowType = $allowType;
    if(!empty($allowSize)) $this->allowSize = $allowSize;
    if(!empty($saveDir)) $this->saveDir = $saveDir;
    if(!empty($isRename)) $this->isRename = $isRename;
  }

  function is_uploaded(){
    if(empty($_FILES[$this->inputName]['name'])){
      $this->chk_err(4);  //       
    }else{
      if(is_uploaded_file($_FILES[$this->inputName]['tmp_name'])){
        return true;
      }else{
        $this->chk_err(-2);    //       
      }
    }
  }

  function chk_type(){
    if(!in_array($_FILES[$this->inputName]['type'],$this->allowType)){
      $this->chk_err(-3);     //           
    }else{
      return true;
    }
  }

  function chk_size(){
    if($_FILES[$this->inputName]['size'] > $this->allowSize){
      $this->chk_(-4);     //       
    }else{
      return true;
    }
  }

  function move_uploaded(){    //      
    if(!$this->is_uploaded()){
      return false;
    }

    if(!$this->chk_size()){
      return false;
    }

    if(!$this->chk_type()){
      return false;
    }

    //   
    if($this->isRename){
      $arrTmp = pathinfo($_FILES[$this->inputName]['name']);
      $extension = strtolower($arrTmp['extension']);
      $file_newname = date("YmdHis").rand(1000,9999)."00.".$extension; //      , 00         
    }else{
      $file_newname = $_FILES[$this->inputName]['name'];
    }
    
    if(!file_exists($this->saveDir)){    //          
      mkdir($this->saveDir,0777,true);  //      
    }

    //    
    $result = move_uploaded_file($_FILES[$this->inputName]['tmp_name'],$this->saveDir."/".$file_newname);

    if($result){
      $path = $this->savePath = $this->saveDir.$file_newname;    //         
      return $path;
    }else{
      $this->chk_err($_FILES[$this->inputName]['error']);
    }
  
  }

  //      
  function chk_err($errID){
    $this->errID = $errID;
    switch($this->errID){
      case -4:
        $this->errMsg = "       ";
        break;
      case -3:
        $this->errMsg = "           ";
        break;
      case -2:
        $this->errMsg = "       ";
        break;
      case -1:
        $this->errMsg = "      ";
        break;
      case 1:
        $this->errMsg = '        php.ini upload_max_filesize      ';
        break;
      case 2:
        $this->errMsg = '          HTML   MAX_FILE_SIZE      ';
        break;
      case 3:
        $this->errMsg = '         ';
        break;
      case 4:
        $this->errMsg = '       ';
        break;
      default:
        break;
    }
    return false;
  
  }

  function get_errMsg(){
    echo $this->errMsg; //      
  }

  /**
   +----------------------------------------------------------
   *       
   *
   +----------------------------------------------------------
   * @static
   * @access public
   +----------------------------------------------------------
   * @param string $image      
   +----------------------------------------------------------
   * @return mixed
   +----------------------------------------------------------
   */
  function getImageInfo($img) {
    $imageInfo = getimagesize($img);
    if( $imageInfo!== false) {
      $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]),1));
      $imageSize = filesize($img);
      $info = array(
        "width"    =>$imageInfo[0],
        "height"  =>$imageInfo[1],
        "type"    =>$imageType,
        "size"    =>$imageSize,
        "mime"    =>$imageInfo['mime'],
      );
      return $info;
    }else {
      return false;
    }
  }

  /**
   +----------------------------------------------------------
   *      
   +----------------------------------------------------------
   * @static
   * @access public
   +----------------------------------------------------------
   * @param string $image   
   * @param string $type     
   * @param string $thumbname       
   * @param string $maxWidth   
   * @param string $maxHeight   
   * @param string $position        
   * @param boolean $interlace       
   * @param boolean $is_save       
   +----------------------------------------------------------
   * @return void
   +----------------------------------------------------------
   */
   
  function thumb($image,$is_save=true,$suofang=0,$type='',$maxWidth=500,$maxHeight=500,$interlace=true){
    //       
    $info = $this->getImageInfo($image);
     if($info !== false) {
      $srcWidth = $info['width'];
      $srcHeight = $info['height'];
      $type = empty($type)?$info['type']:$type;
      $type = strtolower($type);
      $interlace = $interlace? 1:0;
      unset($info);
      if ($suofang==1) {
        $width = $srcWidth;
        $height = $srcHeight;
      } else {
        $scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); //       
        if($scale>=1) {
          //           
          $width  = $srcWidth;
          $height = $srcHeight;
        }else{
          //      
          $width = (int)($srcWidth*$scale);  //147
          $height = (int)($srcHeight*$scale);  //199
        }
      }
      //     
      $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
      $srcImg   = $createFun($image);

      //     
      if($type!='gif' && function_exists('imagecreatetruecolor'))
        $thumbImg = imagecreatetruecolor($width, $height);
      else
        $thumbImg = imagecreate($width, $height);

      //     
      if(function_exists("ImageCopyResampled"))
        imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
      else
        imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
      if('gif'==$type || 'png'==$type) {
        //imagealphablending($thumbImg, false);//         
        //imagesavealpha($thumbImg,true);//        alpha     
        $background_color = imagecolorallocate($thumbImg, 0,255,0); //       
        imagecolortransparent($thumbImg,$background_color); //       ,             
      }
      //  jpeg        
      if('jpg'==$type || 'jpeg'==$type)   imageinterlace($thumbImg,$interlace);
      //$gray=ImageColorAllocate($thumbImg,255,0,0);
      //ImageString($thumbImg,2,5,5,"ThinkPHP",$gray);
      //     
      $imageFun = 'image'.($type=='jpg'?'jpeg':$type); 
      $length = strlen("00.".$type) * (-1);
      $_type = substr($image,-4);
      $length = ($type != $_type ? $length+1 : $length);
      //  
      if ($suofang==1) {
        
        $thumbname01 = substr_replace($image,"01.".$type,$length);    //   
        $thumbname02 = substr_replace($image,"02.".$type,$length);    //   
        $imageFun($thumbImg,$thumbname01,100);
        $imageFun($thumbImg,$thumbname02,100);

        $thumbImg01 = imagecreatetruecolor(190,195);
        imagecopyresampled($thumbImg01,$thumbImg,0,0,$_POST['x'],$_POST['y'],190,195,$_POST['w'],$_POST['h']);

        $thumbImg02 = imagecreatetruecolor(48,48);
        imagecopyresampled($thumbImg02,$thumbImg,0,0,$_POST['x'],$_POST['y'],48,48,$_POST['w'],$_POST['h']);

        $imageFun($thumbImg01,$thumbname01,100);
        $imageFun($thumbImg02,$thumbname02,100);
//        unlink($image);
        imagedestroy($thumbImg01);
        imagedestroy($thumbImg02);
        imagedestroy($thumbImg);
        imagedestroy($srcImg);

        return array('big' => $thumbname01 , 'small' => $thumbname02);  //             
      }else{
        if($is_save == false){                      //       ,            
          $imageFun($thumbImg,$image,100);
        }else{
          $thumbname03 = substr_replace($image,"03.".$type,$length);  //          ,
          $imageFun($thumbImg,$thumbname03,100);

          imagedestroy($thumbImg);
          imagedestroy($srcImg);
          return $thumbname03 ;          //        ,   
        }
      }

     }
     return false;
  }
}

이상 은 본 고의 모든 내용 이 므 로 여러분 이 phop 프로 그래 밍 을 배 우 는 데 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기