[PHP] php 上传文件类 →→→→→进入此内容的聊天室

来自 , 2020-03-29, 写在 PHP, 查看 141 次.
URL http://www.code666.cn/view/512fc3c5
  1. <?php
  2. class Test_Upload{
  3.  
  4.     protected $_uploaded = array();
  5.     protected $_destination;    
  6.     protected $_max = 1024000;
  7.     protected $_messages = array();
  8.     protected $_permited = array(
  9.                                 'image/gif',
  10.                                 'image/jpeg',
  11.                                 'image/pjpeg',
  12.                                 'image/png'    
  13.     );
  14.     protected $_renamed = false;
  15.  
  16.     /**
  17.      *
  18.      * @param mix $path
  19.      *
  20.      */
  21.     public function __construct($path){
  22.  
  23.         if (!is_dir($path) || !is_writable($path)){
  24.             throw new Exception("文件名不可写,或者不是目录!");
  25.         }
  26.         $this->_destination = $path;
  27.         $this->_uploaded = $_FILES;
  28.     }
  29.     /**
  30.      * 移动文件
  31.      *
  32.      */
  33.     public function move(){
  34.  
  35.         $filed = current($this->_uploaded);  
  36.  
  37.         $isOk = $this->checkError($filed['name'], $filed['error']);
  38.         //debug ok
  39.         if ($isOk){
  40.             $sizeOk = $this->checkSize($filed['name'], $filed['size']);
  41.             $typeOk = $this->checkType($filed['name'], $filed['type']);
  42.             if ($sizeOk && $typeOk){
  43.  
  44.                 $success = move_uploaded_file($filed['tmp_name'], $this->_destination.$filed['name']);
  45.  
  46.                 if ($success){
  47.                     $this->_messages[] = $filed['name']."文件上传成功";
  48.                 }else {
  49.                     $this->_messages[] = $filed['name']."文件上传失败";
  50.                 }
  51.             }
  52.  
  53.         }
  54.     }
  55.     /**
  56.      * 查询messages数组内容
  57.      *
  58.      */
  59.     public function getMessages(){
  60.         return $this->_messages;
  61.     }
  62.  
  63.     /**
  64.      *  检测上传的文件大小
  65.      *  @param mix $string
  66.      *  @param int $size
  67.      */
  68.     public function checkSize($filename, $size){
  69.  
  70.         if ($size == 0){
  71.             return false;
  72.         }else if ($size > $this->_max){
  73.             $this->_messages[] = "文件超出上传限制大小".$this->getMaxsize();
  74.             return false;
  75.         }else {
  76.             return true;
  77.         }
  78.     }
  79.  
  80.     /**
  81.      *  检测上传文件的类型
  82.      *  @param mix $filename
  83.      *  @param mix $type
  84.      */
  85.     protected  function checkType($filename, $type){
  86.         if (!in_array($type, $this->_permited)){
  87.             $this->_messages[] = "该文件类型是不被允许的上传类型";
  88.             return false;
  89.         }else {
  90.             return true;
  91.         }
  92.     }
  93.  
  94.     /**
  95.      *  获取文件大小
  96.      *  
  97.      */
  98.     public function getMaxsize(){
  99.         return number_format($this->_max / 1024, 1).'KB';
  100.     }
  101.  
  102.     /**
  103.      * 检测上传错误
  104.      * @param mix $filename
  105.      * @param int $error
  106.      *
  107.      */
  108.     public function checkError($filename, $error){
  109.         switch ($error){
  110.             case 0 : return true;
  111.             case 1 :
  112.             case 2 : $this->_messages[] = "文件过大!"; return true;
  113.             case 3 : $this->_messages[] = "错误上传文件!";return false;
  114.             case 4 : $this->_messages[] = "没有选择文件!"; return false;
  115.             default : $this->_messages[] = "系统错误!"; return false;
  116.         }
  117.     }
  118. }
  119. ?>
  120. //该片段来自于http://yuncode.net
  121.  

回复 "php 上传文件类"

这儿你可以回复上面这条便签

captcha