服务器之家

服务器之家 > 正文

非常经典的PHP文件上传类分享

时间:2021-01-19 15:52     来源/作者:LoeYueng

文件上传是项目开发中比较常见的功能,但文件上传的过程比较繁琐,只要是有文件上传的地方就需要编写这些复杂的代码。为了能在每次开发中降低功能的编写难度,也为了能节省开发时间,通常我们都会将这些反复使用的一段代码封装到一个类中。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
 +-----------------------------------------------------------------------------
 * 文件上传类
 +-----------------------------------------------------------------------------
 * @author Administrator
 +-----------------------------------------------------------------------------
 */
 class FileUpload{
   
  private $filepath//保存路径
  private $allowtype=array('gif','jpg','jpeg','png','txt');
  private $maxsize=1000000;  //最大允许上传大小
  private $israndname=true; //是否随机
  private $orginame; //原始文件名
  private $tmpname//临时文件名
  private $newname//新文件名
  private $filetype; //文件类型 
  private $filesize; //文件大小
  private $errornum=''; //错误号
  private $errormsg; //错误信息
/**
 +------------------------------------------------------------------------------
 *构造函数
 +------------------------------------------------------------------------------
 * @param string $savepath  保存路径
 * @param string $allowtype 允许类型
 * @param string $maxsize  允许大小
 *
 +------------------------------------------------------------------------------
 */   
  function __construct($option=array()){
     foreach ($option as $key=>$value){
        
      if (!in_array($key,get_class_vars(get_class($this)))){
        continue;
       }
      $this->setOption($key, $value); 
     }
  }
  function uploadfile($field) {
     $return=true;
    if (!$this->CheckPath()) {
      $this->errormsg=$this->geterrorNum();
      return false;
    }
    $name=$_FILES[$field]['name'];
    $tmpname=$_FILES[$field]['tmp_name'];
    $filesize=$_FILES[$field]['size'];
    $error=$_FILES[$field]['error']; 
     if (is_array($name)) {
      $errors=array();
       for ($i=0;$i<count($name);$i++){
        if ($this->getFile($name[$i],$tmpname[$i],$filesize[$i],$errors[$i])) {
          if (!$this->CheckSize() && !$this->CheckType()) {
            $errors=$this->getErrorNum();
            return false;
            }                                
        }else{
            $errors=$this->getErrorNum();
            return false;
        }
        if (!$return) {
          $this->getFile();
        }
       }
       if ($return) {
        $fileNames=array();
        for ($i=0;$i<count($name);$i++){
          if ($this->getFile($name[$i], $tmpname[$i], $filesize[$i], $filesize[$i])) {
            $this->SetFileName();
            if (!$this->MoveFile()) {
              $errors[]=$this->getErrorNum();
              $return=false;
            }else{
              $fileNames[]=$this->getNewName();
            }
          }        
        }
        $this->newname=$fileNames;
       }
        $this->errormsg=$errors;
        return $return;
        
     }else
      if($this->getFile($name,$tmpname,filesize,$error)){
        if(!$this->CheckSize()){
          return false;
        }
        if(!$this->CheckType()){
          return false;
        }
         $this->SetFileName();
        if ($this->MoveFile()) {
            return true;
        
      }else{
      return false;
      }
        
      if (!$return) {
      $this->setOption('ErrorNum', 0);
      $this->errormsg=$this->geterrorNum();
      }
     return $return
   }
  }
  /**
   +------------------------------------------------------------------------
   *设置类属性值函数
   +------------------------------------------------------------------------
   * @param mix $key
   * @param mix $value
   */
  private function setOption($key,$value){
    $key=strtolower($key);
    $this->$key=$value;
  }
  /**
   +---------------------------------------------------------------------------
   * 获取文件变量参数函数
   +---------------------------------------------------------------------------
   * @param string $name
   * @param string $tmp_name
   * @param number $size
   * @param number $error
   */
  private function getFile($name,$tmpname,$filetype,$filesize,$error=0){ 
     
    $this->setOption('TmpName', $tmpname);
    $this->setOption('OrgiName', $name);
    $arrstr=explode('.', $name);
    $this->setOption('FileType', $arrstr[count($arrstr)-1]);     
    $this->setOption('FileSize', $filesize);
    return true;
  }
  /**
   +-------------------------------------------------------------------------
   * 检查上传路径函数
   +-------------------------------------------------------------------------
   * @return boolean
   */
  private function CheckPath(){
    if(empty($this->filepath)){
      $this->setOption('ErrorNum', -5);
      return false;
    }
    if (!file_exists($this->filepath)||!is_writable($this->filepath)) {
       if (!@mkdir($this->filepath,0755)) {
         $this->setOption('ErrorNum',-4);
         return false;
       }
    }
    return true;
  }
  private function Is_Http_Post(){
    if (!is_uploaded_file($this->tmpname)) {
      $this->setOption('ErrorNum',-6); 
      return false;
    }else{
      return true;
    }
  }
  /**
   +--------------------------------------------------------------------
   *检查文件尺寸函数
   +--------------------------------------------------------------------
   * @return boolean
   */
  private function CheckSize(){
    if ($this->filesize>$this->maxsize) {
      $this->setOption('ErrorNum', -2);
      return false;
    }else{
      return true;
    }
  }
  /**
   +---------------------------------------------------------------
   * 检查文件类型函数
   +---------------------------------------------------------------
   * @return boolean
   */
  private function CheckType(){
    if (in_array($this->filetype, $this->allowtype)) {
      return true;
    }else{
      $this->setOption('ErrorNum', -1);
      return false;
    }
  }
  private function SetFileName(){
    if ($this->israndname) {
      $this->setOption('NewName', $this->RandName());
    }else{
      $this->setOption('NewName',$this->orginame);
    
  }
  /**
   +-----------------------------------------------------------------
   * 获取新文件名
   +------------------------------------------------------------------
   */
  public function getNewName() {
    return $this->newname;
  }
  private function RandName(){
    $rule=date("YmdHis").rand(0, 999);
    return $rule.'.'.$this->filetype;
  }
  private function MoveFile(){
    if ($this->errornum) {
      $filepath=rtrim($this->filaepath,'/').'/';
      $filepath.=$this->newname;
      if (@move_uploaded_file($this->tmpname,$filepath)) {
        return true;
       }else{
        $this->errormsg=$this->setOption('ErrorNum',-3 );
       }
    }else{
      return false;
    }
  }
  /**
   +----------------------------------------------------------------
   * 错误信息函数
   +----------------------------------------------------------------
   * @return string
   */
   function getErrorNum() {
    $erstr="上传文件<font color='red'>{$this->orginame}</font>出错";
    switch ($this->errornum) {
      case 4:
       $erstr.="没有文件被上传";
        break;
      case 3:
       $erstr.="文件只被部分上传";
        break;
      case 2:
       $erstr.="上传文件超过了HTML表单MAX_FILE_SIZE指定的值";
        break;
      case 1:
       $erstr.="上传文件超过了php.ini配置文件中upload_max_filesize的值";
        break;
      case 0:
       $erstr="上传{$this->orginame}成功";
        break;       
      case -1:
       $erstr="未允许的类型";
        break;
      case -2:
       $erstr.="文件过大,不能超过{$this->maxsize}个字节"
        break;
      case -3:
       $erstr.="上传失败";
        break;
      case -4:
       $erstr="创建上传目录失败,请重新指定上传目录";
        break;
      case -5:
       $erstr="未指定上传路径";
        break;
      case -6:
       $erstr="非法操作";
        break;                   
      default:
       $erstr.="未知错误";
         
    }
    return $erstr;
  }
 }
?>

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部