php文件上传类与详解
      
     
                php 配置中的 upload_tmp_dir 这个参数进行比较,如果文件在这个目录下面,那么 move_uploaded_file 才会进行移动操作,而且这个比较是大小写敏感,/ 在windows 下面也是不一样的,而在php配置文件解析的时候,会调用一个realpath 函数,也就是是说,你在move_uploaded_file 之前.
必须对$file['tmp_name'] = realpath($file['tmp_name']); realpath 一下.
还有一种情况,大家要注意,就是 move_uploaded_file 如果配置成一个无法访问的路径,那么你不管怎么处理,move_uploaded_file 总是不能成功移动文件.
在文件上传的时候,用 move_uploaded_file 这个函数不能移动文件,而用copy 或者 rename 确是可以的.
我也一直很困惑,在文档上,说的比较模糊,就是 move_uploaded_file 这个函数,加了一步检查,检查这个文件是否是有 http post 上传的.
下面我在网上找到一个文件上传类,实例代码如下:
	
	- **文件上传类**/ 
-  
-  
- class upload_file 
-  { 
-    
-   var $upfile_type,$upfile_size,$upfile_name,$upfile; 
-   var $d_alt,$extention_list,$tmp,$arri; 
-   var $datetime,$date; 
-   var $filestr,$size,$ext,$check; 
-   var $flash_directory,$extention,$file_path,$base_directory;  
-   var $url;  
-  
-   function upload_file() 
-   { 
-     
-    $this->set_url("index.php");           
-    $this->set_extention();              
-    $this->set_size(50);               
-    $this->set_date();                
-    $this->set_datetime();              
-    $this->set_base_directory("attachmentfile");   
-   } 
-    
-    
-   function set_file_type($upfile_type) 
-   { 
-    $this->upfile_type = $upfile_type;        
-   } 
-    
-    
-   function set_file_name($upfile_name) 
-   { 
-    $this->upfile_name = $upfile_name;        
-   } 
-    
-    
-   function set_upfile($upfile) 
-   { 
-    $this->upfile = $upfile;             
-   } 
-      
-    
-   function set_file_size($upfile_size) 
-   { 
-    $this->upfile_size = $upfile_size;        
-   } 
-    
-    
-   function set_url($url) 
-   { 
-    $this->url = $url;                
-   } 
-    
-    
-   function get_extention() 
-   { 
-     $this->extention = preg_replace('/.*.(.*[^.].*)*/iu','1',$this->upfile_name);  
-   }  
-        
-    
-   function set_datetime() 
-   { 
-    $this->datetime = date("ymdhis");         
-   } 
-    
-    
-   function set_date() 
-   { 
-    $this->date = date("y-m-d");           
-   } 
-    
-    
-   function set_extention() 
-   { 
-    $this->extention_list = "doc|xls|ppt|avi|txt|gif|jpg|jpeg|bmp|png";  
-   }   
-    
-    
-   function set_size($size) 
-   { 
-    $this->size = $size;               
-   } 
-    
-    
-   function set_base_directory($directory) 
-   { 
-    $this->base_directory = $directory;  
-   } 
-    
-    
-   function set_flash_directory() 
-   { 
-    $this->flash_directory = $this->base_directory."/".$this->date;  
-   } 
-    
-    
-   function showerror($errstr="未知错误!"){ 
-    echo "<script language=网页特效>alert('$errstr');location='javascript:history.go(-1);';</script>"; 
-    exit(); 
-   } 
-    
-    
-   function go_to($str,$url) 
-   { 
-    echo "<script language='javascript'>alert('$str');location='$url';</script>"; 
-    exit(); 
-   } 
-    
-   function mk_base_dir() 
-   { 
-    if (! file_exists($this->base_directory)){    
-     @mkdir($this->base_directory,0777);      
-    } 
-   } 
-    
-   function mk_dir() 
-   { 
-    if (! file_exists($this->flash_directory)){    
-     @mkdir($this->flash_directory,0777);      
-    } 
-   }   
-    
-    
-   function get_compare_extention() 
-   { 
-    $this->ext = explode("|",$this->extention_list); 
-   } 
-    
-    
-   function check_extention() 
-   { 
-    for($i=0;each($this->ext);$i++)             
-    { 
-     if($this->ext[$i] == strtolower($this->extention))  
-     { 
-      $this->check = true;                
-      break; 
-     } 
-    } 
-    if(!$this->check){$this->showerror("正确的扩展名必须为".$this->extention_list."其中的一种!");} 
-     
-   } 
-    
-    
-   function check_size() 
-   { 
-    if($this->upfile_size > round($this->size*1024))      
-    { 
-     $this->showerror("上传附件不得超过".$this->size."kb");  
-    } 
-   } 
-    
-   function set_file_path() 
-   { 
-    $this->file_path = $this->flash_directory."/".$this->datetime.".".$this->extention;  
-   } 
-    
-    
-   function copy_file() 
-   { 
-    if(copy($this->upfile,$this->file_path)){         
-     print $this->go_to("文件已经成功上传!",$this->url);   
-    }else { 
-     print $this->showerror("意外错误,请重试!");      
-    } 
-   } 
-    
-    
-   function save() 
-   { 
-    $this->set_flash_directory();   
-    $this->get_extention();      
-    $this->get_compare_extention();  
-    $this->check_extention();     
-    $this->check_size();       
-    $this->mk_base_dir();       
-    $this->mk_dir();         
-    $this->set_file_path();      
-    $this->copy_file();        
-   } 
-    
-  } 
调用方法,实例代码如下,upload.htm @ 表单文件名:
	
	- <html> 
- <head> 
-   <title>文件上传实例</title> 
- </head> 
- <body> 
-  <form action="test.php" method="post" enctype="multipart/form-data"> 
- <table border=0 cellpadding=3 cellspacing=4 width=30%> 
-  <tr> 
-   <td width=10% nowrap>附件来源</td> 
-   <td><input name="src" type="file"/></td> 
-  </tr> 
-  <tr>  
-   <td colspan=2 align=center><input type="submit" value="上传"></td>  
-  </tr> 
- </table> 
- </form> 
- </body> 
- </html> 
test.php @ 处理表单文件名,实例代码如下:
	
	- <?php 
-   include("upload.php");                 # 加入类文件 
-    $f_upload = new upload_other;             # 创建对象 
-    $f_upload->set_file_type($_files['src']['type']);   # 获得文件类型 
-    $f_upload->set_file_name($_files['src']['name']);   # 获得文件名称 
-    $f_upload->set_file_size($_files['src']['size']);   # 获得文件尺寸 
-    $f_upload->set_upfile($_files['src']['tmp_name']);  # 服务端储存的临时文件名 
-    $f_upload->set_size(100);               # 设置最大上传kb数 
-     $f_upload->set_base_directory("uploadimages");    # 文件存储根目录名称 
-    $f_upload->set_url("up.php");             # 文件上传成功后跳转的文件 
-    $f_upload->save();                  # 保存文件   
-    ?> 
没有在类里做有无文件的验证,大家可以在前台搞定.