<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Up_file  extends HC_Controller
{
	//注意无须构造函数,使用了构造函数得重构父类 parent::__construct();
	function __construct()
	{
		parent::__construct();
		$this -> load_lang('adminsys/up_file');
		$this -> load_db();
		$this -> load('admin'); 
		$this -> admin -> online_admin();
		//$this -> load('helper');
	}
	
	function add(  )
	{
		$this -> view ->  caching = true;
		$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24

		$dirval 	= $this -> input -> get["dir"];
		$ret_input  = $this -> input -> get["ret_input"];
		
		$this -> view -> assign('dir',$dirval);
		$this -> view -> assign('ret_input',$ret_input);
		$this -> view -> display('up_file_add.tpl');
	}

	function add_file()
	{
		$dirval 	= $this -> input -> post["dir"];
		$ret_input  = $this -> input -> post["ret_input"];
		
		$configUp = array();
		
		$configUp['type']	= array("flash","img"); //上传允许type值
		
		$configUp['img'] 	= array("jpg","bmp","gif","png"); //img允许后缀
		
		$configUp['flash'] 	= array("flv","swf"); //flash允许后缀
		
		$configUp['office'] = array("doc","docx","docm","dotx","dotm",
								"xls","xlsx","xlsm","xltm","xlsb","xlam","csv","xlw","wk4","wk3","wk1","wks","dbf",
								"ppt","pptx","pptm","ppsx","potx","potm","ppam"); //office允许后缀
		
		if(MAX_UPSIZE != '')
		{
			$configUp['flash_size'] = MAX_UPSIZE; //上传flash大小上限 单位：KB
			$configUp['img_size'] = MAX_UPSIZE; //上传img大小上限 单位：KB
		}else{
			$configUp['flash_size']	=	1000; //上传flash大小上限 单位：KB
			$configUp['img_size']	=	2000; //上传img大小上限 单位：KB
		}
		
		$configUp['message']	= "上传成功"; //上传成功后显示的消息，若为空则不显示
		$configUp['name']		= mktime(); //上传后的文件命名规则 这里以unix时间戳来命名
		
		if(BASE_WEBURL!='')
		{
			$configUp['flash_dir']	= BASE_WEBURL."/uploads/"; //上传flash文件地址 采用绝对地址 方便upload.php文件放在站内的任何位置 后面不加"/"
			$configUp['img_dir']		= BASE_WEBURL."/uploads/"; //上传img文件地址 采用绝对地址 采用绝对地址 方便upload.php文件放在站内的任何位置 后面不加"/"
		}else{
			$configUp['flash_dir']	=  "/uploads/"; //上传flash文件地址 采用绝对地址 方便upload.php文件放在站内的任何位置 后面不加"/"
			$configUp['img_dir']		= "/uploads/"; //上传img文件地址 采用绝对地址 采用绝对地址 方便upload.php文件放在站内的任何位置 后面不加"/"
		}
		
		if(IMG_URL!='')
		{
			$configUp['site_url'] = IMG_URL; //网站的网址 这与图片上传后的地址有关 最后不加"/" 可留空
		}else{
			$configUp['site_url']=""; //网站的网址 这与图片上传后的地址有关 最后不加"/" 可留空
		}
		if($dirval !=''){
			$configUp['img_dir'] = $configUp['img_dir'].$dirval;
		}
	
	
		if(is_uploaded_file($_FILES['upload_file']['tmp_name']))
		{
		   //判断上传文件是否允许
		   $filearr=pathinfo($_FILES['upload_file']['name']);
		   $filetype=$filearr["extension"];
	
	
		   if(!in_array($filetype,$configUp['img']))
			$this -> echo_js("","错误的文件类型！");
		   //判断文件大小是否符合要求
		   if($_FILES['upload_file']['size'] > $configUp["img_size"]*1024)
			$this -> echo_js("","上传的文件不能超过".$configUp["img_size"]."KB！");
	 
		   
		   $file_abso=$configUp["img_dir"]."/".$configUp['name'].".".$filetype;
		   $file_host=$_SERVER['DOCUMENT_ROOT'].$file_abso;
		   if(move_uploaded_file($_FILES['upload_file']['tmp_name'],$file_host))
		   {
				$this -> echo_js($file_abso,$configUp['message'],$is_success = 1 );
		   }else
		   {
				$this -> echo_js("","文件上传失败，请检查上传目录设置和目录读写权限");
		   }
		}
	}

	//输出js调用
	function echo_js($fileurl, $message , $is_success = 0)
	{
			echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		if($message!='' and $is_success == 0 ){
			exit($message);
		}elseif($fileurl!='' and $is_success == 1 ){
			//window.opener.document.getElementById('bra_logo').value=
			echo $str="<script type=\"text/javascript\">window.opener.document.getElementById('bra_logo').value='".$fileurl."';window.opener = null;window.open('','_top');window.top.close(); </script>";
			exit($message);
		}
	}
}
/*
   		window.opener.document.getElementById('bra_logo').value=imgroot_url+document.getElementById('dir').value+'/'+file_url;
	    window.opener = null;
	    window.open('','_top');
	    window.top.close(); 
*/

//文件上传
//uploadfile();

?>