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

yipeiwu_com5年前PHP代码库

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

<?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;
    }
  }
}
?>

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

相关文章

PHP的AES加密算法完整实例

本文实例讲述了PHP的AES加密算法。分享给大家供大家参考,具体如下: <?php class MCrypt { private $hex_iv = '000000...

Opcache导致php-fpm崩溃nginx返回502

我这个博客为了提高运行效率在vps上装了opcache扩展,结果发现有个页面返回502,其他页面正常。 检查了php-fpm日志,发现是php-fpm子进程不知道为什么会崩溃,然后把op...

PHP获取163、gmail、126等邮箱联系人地址【已测试2009.10.10】

PHP获取163、gmail、126等邮箱联系人地址【已测试2009.10.10】

在网上找了一些,大部分都已经失效,为此我重新整理了一下;特别放出126的代码,163是比较容易抓取的;126有点变态多了一次跳转,比较麻烦复制代码 代码如下:<?php /** *...

需要注意的几个PHP漏洞小结

需要注意的几个PHP漏洞小结

需要注意的几个PHP漏洞 几个重要的php.ini选项   Register Globals   php>=4.2.0,php.ini的register_globals选项的默认值...

PHP 上传文件的方法(类)

复制代码 代码如下: /** * 图片上传方法 * $maxsize=500000 = 500k; * $updir="up/"; * $upfile=$_FILES["file_img...