php简单上传类 | upload.class.php

字体大小: 中小 标准 ->行高大小: 标准

使用方法:
$file = new upload;
$file->uploadFile('face', rand().'/');  
第二个参数是用来灵活定义存放目录的(比如可以给每一个用户按照用户UID来存放),当然可默认为空(存放地址就按照配置方法中的地址)

 

<?php 

   

 header('Content-Type:text/html; charset=utf-8'); 

   

 class upload{ 

       

     private function conf($key){ 

         $conf = array( 

             'size' => 2000000, 

             'type' => 'jpg|jpeg|gif|png', 

             'path' => './images/face/', 

             'rename' => uniqid().gmdate('YmdHis') 

         ); 

         return $conf[$key]; 

     } 

       

     private function filext($name){ 

         return strrchr($name, '.');  

     } 

       

     private function direname($rename){ 

         $dirname = dirname($rename); 

         if(is_dir($dirname)){ 

             return $rename;  

         }else{ 

             @mkdir($dirname, 0755, true); 

             return $rename;  

         } 

     } 

       

     public function uploadFile($fileName, $userDiyPath = ''){ 

         $tmp = $_FILES[$fileName]; 

         if(is_uploaded_file($tmp['tmp_name'])){ 

             if(preg_match('/'.$this->conf('type').'/i', $tmp['type'])){ 

                 if($tmp['size'] < $this->conf('size')){ 

                     if($tmp['error'] == 0){ 

                         $rename = $this->direname($this->conf('path').$userDiyPath.$this->conf('rename').$this->filext($tmp['name'])); 

                         if(@move_uploaded_file($tmp['tmp_name'], $rename)){ 

                             return $rename;  

                         }else{ 

                             js::reload('文件上传出现错误!');         

                         } 

                     }else{ 

                         js::reload('文件上传出现错误!');     

                     } 

                 }else{ 

                     js::reload('文件上传大小错误!');         

                 } 

             }else{ 

                 js::reload('文件上传类型错误!');     

             } 

         }else{ 

             js::reload('文件上传方式错误!'); 

         } 

     } 

           

 } 

   

 class js{ 

   

     public static function reload($msg){ 

         exit($msg);  

     } 

       

 } 

   

   

 $file = new upload; 

 $file->uploadFile('face', rand().'/');

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/29897.html