搜索
首页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

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具