多文件上传类

<?php
//文件上传类
class upload{
    
    protected $allowExt = array('jpg','jpeg','gif','png','bmp');    //定义允许上传的类型
    protected $max_size = 2;    //允许最大上传文件大小
    protected $fpath = array();
    protected $errorno = 0;        //错误号
    
    protected $error = array(
        0=>'上传完成',
        1=>'文件超出upload_max_filesize',
        2=>'文件超出表单中 MAX_FILE_SIZE 选项指定的值',
        3=>'文件只有部分被上传',
        4=>'没有文件被上传',
        6=>'找不到临时目录',
        7=>'文件定入失败',
        8=>'文件大小超出配置文件的限制',
        9=>'不允许的文件类型',
        10=>'创建目录失败',
        11=>'失败'
    );

    public function up($name){
        $this->fpath = array();
        $this->upda($name);
        return $this->fpath;
    }

    public function upda($name){
        
        $files = $_FILES[$name];

        //多文件上传,for循环来上传
        //得到数组的长度
        $count = count($files['name']);
        
        

        for($i = 0; $i <$count;$i++ ){
            
            if($files['error'][$i] > 0){
                return false;
            }
            
            //获得文件扩展名
            $ext = $this->getExt($files['name'][$i]);
            
            //判断扩展名是否允许
            if(!$this->checkExt($ext)){                
                $this->errorno = 9;
                return false;
            }
            
            //判断文件是否超出配置大小
            if(!$this->checkSize($files['size'][$i])){
                $this->errorno = 8;
                return false;
            }
            //获得上传目录
            $path = $this->mk_dir();
            if(!$path){
                $this->errorno = 10;
                return false;
            }

            //获得文件名
            $fname = $this->getName();
            $fname = $path . '/' . $fname . $ext;

            //将临时文件移到指定目录
            if(!move_uploaded_file($files['tmp_name'][$i],$fname)){
                $this->errorno = 11;
                return false;
            }

            $fpath = str_replace(ROOT,'',$fname);
            $this->fpath['ori_img'.($i+1)] = $fpath;
            
        }

    } 


    
    //获得文件的扩展名
    protected function getExt($filename){
        
        $ext = strtolower(strrchr($filename,'.'));
        
        return $ext;
    }


    //检查文件扩展名
    protected function checkExt($ext){
        return in_array(trim($ext,'.'),$this->allowExt);
    }

    //检查文件大小
    protected function checkSize($size){
        return $size = $this->max_size * 1000 *1000;
    }

    //按日期生成目录
    protected function mk_dir(){
        $path = ROOT . 'data/images/' . date('Y/m/d',time());

        if(!is_dir($path)){
            if(!mkdir($path,0777,true)){
                return false;
            }
        }
        return $path;
    }

    //生成随机文件名
    protected function getName($n = 8){
        if($n<=0){
            return false;
        }
        $str = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ0123456789';

        $str = date('Ymd',time()).substr(str_shuffle($str),0,$n);
        return $str;

    }

}

?>

 

posted @ 2012-10-15 16:34  探索者_PHP  阅读(239)  评论(0编辑  收藏  举报