搜尋
首頁php教程php手册适用于初学者的简易PHP文件上传类,

适用于初学者的简易PHP文件上传类,

本文实例讲述了PHP多文件上传类,分享给大家供大家参考。具体如下:

<&#63;php
class 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;
    }
  }
}
&#63;>

希望本文所述对大家的php程序设计有所帮助。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器