php创建文件夹上传图片

<?php
/**
*文件上传类 Upload
*/
class Upload {
    
    const UPLOAD_SECCUSS        = "上传成功!请进入下一步..." ;
    const UPLOAD_FAILING        = "上传失败!" ;
    const UPLOAD_ERRORS            = "上传图片失败,是图片文件为空、文件格式不正确,或是文件超过了限大小" ;
    const NOT_UPLOAD_FILE        = "不是上传的文件,你想干嘛!!" ;
    var $id ;    //id关联编号 
    var $dirStyle = 0 ; //设置文件上传目录方式0:mdir方式 ;1 :mdir_YM 方式 ; 2:mdir_YM_D 方式
    var $size ;     //文件大小 单位:字节
    var $type ;      //文件类型
    var $path ;    //保存回路径
    var $upfile ; //上传文件数组
    //var $name ;   //form中 "type=file" 输入框的name值
    var $isVaild=false ; //验证
    var $isSuccess=false ;      //是否成功
    //var $newpath ; // 已处理过的路径
    var $newfilename ; //已处理过的文件名

    const MY_DS = '/'; //路径分隔符 通用的
    
    function __construct($upfile,$path,$type="image/pjpeg,image/jpg,image/jpeg,image/gif,image/x-png,image/png,image/bmp",$size=50000){
        $this->path = $path ;
        $this->type = explode(',',$type) ;
        $this->size = $size ;
        $this->upfile = $upfile ;
    }
    
    /**
     *用id号关联文件
     */
    function setId($id){
        $this->id = $id ;
    }
    
    /**
     *设置文件上传目录方式
     */
    function setDirStyle($id=1){
        if($id == 1){
            return ;
        } else {
            $this->dirStyle = 2 ;
        }    
    }
    
    /**
     *验证文件是否可以上传
     */
    function vaild(){
        foreach( $this->type as $value ){
            if( $this->upfile['type'] == $value && $this->upfile['size'] > 0 && 
                $this->upfile['size'] <= $this->size ){
                $this->isVaild = true ;
                return true ;
            }
        }
        return $this->isVaild ;
    }
    
    /**
     *判断是否上传成功
     */
    function isSuccess(){
        return $this->isSuccess ;
    }
    
    /**
     *分年月建立日录
     */
    function mdir_YM($path){
        //$dir = date("Y-m") ;
        $dir = date("Ym") ;
        $path = str_replace(array('\'),self::MY_DS,$path) ;
        if( substr($path,-1,1) != self::MY_DS ){
            $path .= self::MY_DS ;
        }
        if(! file_exists($path.$dir )){
            if( mkdir($path.$dir) ){
                $this->path= $path.$dir.self::MY_DS ;
            }
        }else{
            $this->path= $path.$dir.self::MY_DS ;
        }
    }
    
    /**
     *分年月/日建立日录
     */
    function mdir_YM_D($path){
        //$dir = date("Y-m") ;
        $dir = date("Ym") ;
        $subdir = date("d") ;
        $path = str_replace(array('\'),self::MY_DS,$path);
        
        if ( substr($path,-1,1) != self::MY_DS ){
            $path .= self::MY_DS ;
        }
        
        $dirPath = $path.$dir ;
        $subDirPath = $dirPath.self::MY_DS.$subdir ;
        
        if (! file_exists( $dirPath )){
            if ( mkdir( $dirPath ) ){
                if (file_exists($dirPath ) && !file_exists( $subDirPath )){
                    if (mkdir( $subDirPath )){
                        $this->path= $subDirPath.self::MY_DS ;        
                    }
                }
            }
        } else if (!file_exists( $subDirPath )) {
            if (mkdir( $subDirPath )){
                        $this->path= $subDirPath.self::MY_DS ;        
            }
        } else {
            $this->path = $subDirPath.self::MY_DS ;
        }
    }
    
    
    /**
     *根据路径创建目录
     *@param $path 带文件名的路和径
     */
    static function mdirPath($path){
        $thePath = str_replace(array('\'),self::MY_DS,$path);
        $thePath = dirname(substr_compare($thePath,self::MY_DS,0,1) == 0 ? substr($thePath,1) : $thePath );
        $sub = explode(self::MY_DS,$thePath) ;
        $n = count($sub) ;
        $i = 0 ;
        $tempPath = '';
        
        while($i < $n){
            $tempPath .= $sub[$i++].self::MY_DS ;     
            if (!file_exists($tempPath)) {
                if (! mkdir($tempPath)) {
                    return false ;
                } 
            }
        }
        
        if($i >= $n){
            return true;
        }
    }
    
    /**
     *(static)
     *分年月/日建立日录
     */
    static function sMdir_YM_D($path){
        //$dir = date("Y-m") ;
        $dir = date("Ym") ;
        $subdir = date("d") ;
        $path = str_replace(array('\'),self::MY_DS,$path) ;
        
        if( substr($path,-1,1) != self::MY_DS ){
            $path .= self::MY_DS ;
        }
        
        $dirPath = $path.$dir ;
        $subDirPath = $dirPath.self::MY_DS.$subdir ;
        
        if (! file_exists( $dirPath )){
            if ( mkdir( $dirPath ) ){
                if (file_exists( $dirPath ) && !file_exists( $subDirPath )){
                    if (mkdir( $subDirPath )){
                        return $subDirPath.self::MY_DS ;
                    } else {
                        return '' ;
                    }
                } else {
                    return $subDirPath.self::MY_DS ;
                }
            } else {
                return '' ;
            }
        }else if (!file_exists( $subDirPath )){
                    if (mkdir( $subDirPath )){
                        return $subDirPath.self::MY_DS ;
                    } else {
                        return '' ;
                    }
        } else {
            return $subDirPath.self::MY_DS ;
        }
    }
    
    /**
     *返回文件路径名,不包括文件名
     */
    function getPath(){
        return $this->path;
    }
    
    /**
     *返回文件保存的完整路径
     */
    function getFullPath(){
        return (substr_compare($this->path,self::MY_DS,0,1) != 0 ? self::MY_DS.$this->path : $this->path).$this->newfilename;    
    }
    
    /**
     *产生新的文件名
     */
    function mfilename($name){
        $names = explode(".",$name);
        $theId = !empty($this->id) ? $this->id.'_' : '' ;
        $this->newfilename= $theId.uniqid(rand()).".".$names[count($names)-1];
    }
    
    /**
     *返回新的文件名
     */
    function getNewFilename(){
        return $this->newfilename;
    }
    
    /**
     *开始上传
     */
    function doUpload(){
        if($this->vaild()){
            if(is_uploaded_file($this->upfile['tmp_name'])){
                if($this->dirStyle == 1){
                    $this->mdir_YM($this->path);
                } else if($this->dirStyle == 2) {
                    $this->mdir_YM_D($this->path);
                } else {
                    //$this->path = substr_compare($this->path,self::MY_DS,0,1) != 0 ? self::MY_DS.$this->path : $this->path ;
                }
                
                $this->mfilename($this->upfile['name']);
                if (is_uploaded_file($this->upfile['tmp_name'])){
                    if(move_uploaded_file($this->upfile['tmp_name'],$this->path.$this->getNewFilename())){
                        $msg = self::UPLOAD_SECCUSS ;
                        $this->isSuccess = true;
                    }else{
                        $msg = self::UPLOAD_FAILING ;
                    }
                }
                
            } else {
                $msg = self::NOT_UPLOAD_FILE ;
            }
        }else{
               $msg = self::UPLOAD_ERRORS. round($this->size/1024) ."KB." ;
        }
        return $msg ;
    }

    
}
?>

posted @ 2016-05-14 21:12  小美好。  阅读(611)  评论(0编辑  收藏  举报