<?php 
defined('G_IN_SYSTEM')or exit('');
System::load_app_class('admin','','no');
class upload extends admin {
	public function __construct(){
		parent::__construct();
		$this->db=$this->DB();
		$this->ment=array(
						array("file","上传文件管理",ROUTE_M.'/'.ROUTE_C."/lists"),
		);
	}
	
	//文件读取
	public function lists(){		
		if($this->segment(4)){
			 $dir=trim($this->segment(4),'-');
			 $dir=str_replace("-",DIRECTORY_SEPARATOR,$dir);
			 $dirpath = G_UPLOAD.$dir;
			 $ipath=G_MODULE_PATH.'/upload/lists/'.$this->segment(4);
			 $opath=G_UPLOAD_PATH.'/'.$dir;
		}else{
			$dirpath = G_UPLOAD;
			$ipath=G_MODULE_PATH.'/upload/lists/';	
			$opath=G_UPLOAD_PATH;
		}		
	
		$arr=array();
		if(file_exists($dirpath)){
			if ($dh = opendir($dirpath)){
				while (($file = readdir($dh)) !== false){					
					$file=mb_convert_encoding ($file,'UTF-8','GBK');
					if($file!="." && $file!=".."){						
						if(is_dir($dirpath.DIRECTORY_SEPARATOR.$file)){							
							$arr[]=array("type"=>"目录","name"=>$file,"url"=>$ipath.$file.'-');
						}else{
							$arr[]=array("type"=>"文件","name"=>$file,"url"=>$opath.'/'.$file);					
						}						
					}
				}
				closedir($dh);
			}
		}		
		include $this->tpl(ROUTE_M,'upload.lists');	
	}
	
	
	public function upload(){
		//文件名
		$file_name=empty($_REQUEST['html5md5'])?md5_file($_FILES["file"]["tmp_name"]):$_REQUEST['html5md5'];
		//文件夹
		$folder="/statics/uploads/shopimg/".$file_name{0}.$file_name{1}.'/'.$file_name{2}.$file_name{3}.'/';
		if(!is_dir(MYROOT.$folder)){
			mkdirs(MYROOT.$folder);	
		}
		//文件类型
		$file_exts=".".pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
		$file_url=$folder.$file_name.$file_exts;
		
		$re=move_uploaded_file($_FILES["file"]["tmp_name"], MYROOT.$file_url);
		if($re){
			$url=str_replace('/statics/uploads/', '', $file_url);
			$data=array(
				'url'=>$url
			);
		    header('Content-Type:application/json; charset=utf-8');
            exit(json_encode($data));
		}
	}
	
}//
?>