php实现文件上传

时间:2022-05-07
本文章向大家介绍php实现文件上传,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

存档:

upload1.html(单文件上传)

 1 <html>
 2     <head>
 3         <title>单个文件上传</title>
 4     </head>
 5     <body>
 6         <form action="upload.php" method="post" enctype="multipart/form-data">
 7             <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
 8             选择文件:<input type="file" name="myfile">
 9             <input type="submit" value="上传文件">
10         </form>
11     </body>
12 </html>

upload2.html(多文件上传)

 1 <html>
 2     <head>
 3         <title>多文件上传</title>
 4     </head>
 5     <body>
 6         <form action="upload.php" method="post" enctype="multipart/form-data">
 7             <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
 8             选择文件1:<input type="file" name="myfile[]"><br>
 9             选择文件2:<input type="file" name="myfile[]"><br>
10             选择文件3:<input type="file" name="myfile[]"><br>
11             <input type="submit" value="上传文件">
12         </form>
13     </body>
14 </html>

upload.php

 1 <?php
 2     require "fileupload.class.php";
 3     $up=new FileUpload;
 4     $up ->set('path','./newpath/')
 5         ->set('size',1000000)
 6         ->set('allowtype',array('gif','jpg','png'))
 7         ->set('israndname',false);
 8     if($up->upload('myfile')){
 9         print_r($up->getFileName());
10     }
11     else{
12         print_r($up->getErrorMsg());
13     }
14 ?>

fileupload.class.php

  1 <?php
  2     class FileUpload{
  3         private $path="./uploads";
  4         private $allowtype=array('jpg','gif','png');
  5         private $maxsize=1000000;
  6         private $israndname=true;
  7         private $originName;
  8         private $tmpFileName;
  9         private $fileType;
 10         private $fileSize;
 11         private $newFileName;
 12         private $errorNum=0;
 13         private $errorMess="";
 14         
 15         function set($key,$val){
 16             $key=strtolower($key);
 17             if(array_key_exists($key,get_class_vars(get_class($this)))){
 18                 $this->setOption($key,$val);
 19             }
 20             return $this;
 21         }
 22         
 23         function upload($fileField){
 24             $return=true;
 25             if(!$this->checkFilePath()){
 26                 $this->errorMess=$this->getError();
 27                 return false;
 28             }
 29             $name=$_FILES[$fileField]['name'];
 30             $tmp_name=$_FILES[$fileField]['tmp_name'];
 31             $size=$_FILES[$fileField]['size'];
 32             $error=$_FILES[$fileField]['error'];
 33             if(is_Array($name)){
 34                 $errors=array();
 35                 for($i=0;$i<count($name);$i++){
 36                     if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
 37                         if(!$this->checkFileSize()||!$this->checkFileType()){
 38                             $errors[]=$this->getError();
 39                             $return=false;
 40                         }
 41                     }
 42                     else{
 43                         $errors[]=$this->getError();
 44                         $return=false;
 45                     }
 46                     if(!$return){
 47                         $this->setFiles();
 48                     }
 49                 }
 50                 if($return){
 51                     $fileNames=array();
 52                     for($i=0;$i<count($name);$i++){
 53                         if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
 54                             $this->setNewFileName();
 55                             if(!$this->copyFile()){
 56                                 $errors[]=$this->getError();
 57                                 $return=false;
 58                             }
 59                             $fileNames[]=$this->newFileName;
 60                         }
 61                     }
 62                     $this->newFileName=$fileNames;
 63                 }
 64                 $this->errorMess=$errors;
 65                 return $return;
 66             }
 67             else{
 68                 if($this->setFiles($name,$tmp_name,$size,$error)){
 69                     if($this->checkFileSize()&&$this->checkFileType()){
 70                         $this->setNewFileName();
 71                         if($this->copyFile()){
 72                             return true;
 73                         }
 74                         else{
 75                             $return=false;
 76                         }
 77                     }
 78                     else{
 79                         $return=false;
 80                     }
 81                 }
 82                 else{
 83                     $return=false;
 84                 }
 85                 if(!$return){
 86                     $this->errorMess=$this->getError();
 87                 }
 88                 return $return;
 89             }
 90         }
 91         
 92         public function getFileName(){
 93             return $this->newFileName;
 94         }
 95         
 96         public function getErrorMsg(){
 97             return $this->errorMess;
 98         }
 99         
100         private function getError(){
101             $str="上传文件<font color='red'>{$this->originName}</font>时出错:";
102             switch($this->errorNum){
103                 case 4:
104                     $str.="没有文件被上传";
105                     break;
106                 case 3:
107                     $str.="文件只有部分被上传";
108                     break;
109                 case 2:
110                     $str.="上传的文件大小超过了HTML表单中MAX_FILE_SIZE选项指定的值";
111                     break;
112                 case 1:
113                     $str.="上传的文件超过了php.ini中的upload_max_filesize选定限制的值";
114                     break;
115                 case -1:
116                     $str.="未允许类型";
117                     break;
118                 case -2:
119                     $str.="文件过大,上传的文件不能超过{$this->maxsize}个字节";
120                     break;
121                 case -3:
122                     $str.="上传失败";
123                     break;
124                 case -4:
125                     $str.="建立存放上传文件目录失败,请重新指定上传目录";
126                     break;
127                 case -5:
128                     $str.="必须指定上传文件的路径";
129                     break;
130                 default:
131                     $str.="未知错误";
132                     //break;
133             }
134             return $str.'<br>';
135         }
136         
137         private function setFiles($name="",$tmp_name="",$size=0,$error=0){
138             $this->setOption('errorNum',$error);
139             if($error){
140                 return false;
141             }
142             $this->setOption('originName',$name);
143             $this->setOption('tmpFileName',$tmp_name);
144             $aryStr=explode(".",$name);
145             $this->setOption('fileType',strtolower($aryStr[count($aryStr)-1]));
146             $this->setOption('fileSize',$size);
147             return true;
148         }
149         
150         private function setOption($key,$val){
151             $this->$key=$val;
152         }
153         
154         private function setNewFileName(){
155             if($this->israndname){
156                 $this->setOption('newFileName',$this->proRandName());
157             }
158             else{
159                 $this->setOption('newFileName',$this->originName);
160             }
161         }
162         
163         private function checkFileType(){
164             if(in_array(strtolower($this->fileType),$this->allowtype)){
165                 return true;
166             }
167             else{
168                 $this->setOption('errorNum',-1);
169                 return false;
170             }
171         }
172         
173         private function checkFileSize(){
174             if($this->fileSize>$this->maxsize){
175                 $this->setOption('errorNum',-2);
176                 return false;
177             }
178             else{
179                 return true;
180             }
181         }
182         
183         private function checkFilePath(){
184             if(empty($this->path)){
185                 $this->setOption('errorNum',-5);
186                 return false;
187             }
188             if(!file_exists($this->path)||!is_writable($this->path)){
189                 if(!@mkdir($this->path,0755)){
190                     $this->setOption('errorNum',-4);
191                     return false;
192                 }
193             }
194             return true;
195         }
196         
197         private function proRandName(){
198             $fileName = date('YmdHis')."_".rand(100,999);
199             return $fileName.'.'.$this->fileType;
200         }
201         
202         private function copyFile(){
203             if(!$this->errorNum){
204                 $path=rtrim($this->path,'/').'/';
205                 $path.=$this->newFileName;
206                 if(@move_uploaded_file($this->tmpFileName,$path)){
207                     return true;
208                 }
209                 else{
210                     $this->setOption('errorNum',-3);
211                     return false;
212                 }
213             }
214             else{
215                 return false;
216             }
217         }
218     }
219 ?>

结果如下: