適用於初學者的簡易PHP檔案上傳類_php技巧

來源:互聯網
上載者:User

本文執行個體講述了PHP多檔案上傳類,分享給大家供大家參考。具體如下:

<?phpclass Test_Upload{   protected $_uploaded = array();  protected $_destination;    protected $_max = 1024000;  protected $_messages = array();  protected $_permited = array(                'image/gif',                'image/jpeg',                'image/pjpeg',                'image/png'    );  protected $_renamed = false;     /**   *    * @param mix $path   *    */  public function __construct($path){         if (!is_dir($path) || !is_writable($path)){      throw new Exception("檔案名稱不可寫,或者不是目錄!");    }    $this->_destination = $path;    $this->_uploaded = $_FILES;  }  /**   * 移動檔案   *    */  public function move(){         $filed = current($this->_uploaded);            $isOk = $this->checkError($filed['name'], $filed['error']);    //debug ok    if ($isOk){      $sizeOk = $this->checkSize($filed['name'], $filed['size']);      $typeOk = $this->checkType($filed['name'], $filed['type']);      if ($sizeOk && $typeOk){                 $success = move_uploaded_file($filed['tmp_name'], $this->_destination.$filed['name']);                 if ($success){          $this->_messages[] = $filed['name']."檔案上傳成功";        }else {          $this->_messages[] = $filed['name']."檔案上傳失敗";        }      }           }  }  /**   * 查詢messages數組內容    *   */  public function getMessages(){    return $this->_messages;  }     /**   * 檢測上傳的檔案大小   * @param mix $string   * @param int $size   */  public function checkSize($filename, $size){         if ($size == 0){      return false;    }else if ($size > $this->_max){      $this->_messages[] = "檔案超出上傳限制大小".$this->getMaxsize();      return false;    }else {       return true;    }  }     /**   * 檢測上傳檔案的類型   * @param mix $filename   * @param mix $type   */  protected function checkType($filename, $type){    if (!in_array($type, $this->_permited)){      $this->_messages[] = "該檔案類型是不被允許的上傳類型";      return false;    }else {      return true;    }  }     /**   * 擷取檔案大小   *    */  public function getMaxsize(){    return number_format($this->_max / 1024, 1).'KB';  }     /**   * 檢測上傳錯誤   * @param mix $filename   * @param int $error   *    */  public function checkError($filename, $error){    switch ($error){      case 0 : return true;      case 1 :      case 2 : $this->_messages[] = "檔案過大!"; return true;      case 3 : $this->_messages[] = "錯誤上傳檔案!";return false;      case 4 : $this->_messages[] = "沒有選擇檔案!"; return false;      default : $this->_messages[] = "系統錯誤!"; return false;    }  }}?>

希望本文所述對大家的php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.