<?php 
function dir_create($msg_path, $msg_mode = 0777)
{
	$return_val="";
	$str_path=$msg_path;
	if(substr($str_path, -1) != '/') $str_path = $str_path.'/';
	if(is_dir($str_path)) return "目录已经存在!";
	$arr_temp = explode('/', $str_path);
	$cur_dir = '';
	$int_max = count($arr_temp) - 1;
	for($i=0; $i<$int_max; $i++)
	{
		$cur_dir .= $arr_temp[$i].'/';
		if(is_dir($cur_dir)) continue;
		@mkdir($cur_dir, $msg_mode);
		//@chmod($cur_dir, $msg_mode);
	}
	return $return_val;
}

function dir_copy($msg_fromdir,$msg_name,$msg_todir)
{
	$arr_return=array("err"=>"","name"=>"");
	$return_val="";
	$str_fromdir =$msg_fromdir;
	$str_todir = $msg_todir;
	if(!is_dir($str_fromdir)){
		$arr_return["err"]="原目录不存在!";
		return $arr_return;
	}
	if(!is_dir($str_todir)) dir_create($str_todir);
	if(substr($str_fromdir,-1)!="/") $str_fromdir=$str_fromdir."/";
	if(substr($str_todir,-1)!="/") $str_todir=$str_todir."/";
	$i=0;
	$ii="";
	$str_topath=$str_todir.$msg_name;
	while(is_dir($str_topath))
	{
		if($i==0)
		{
			$str_topath=$str_todir."复件 ".$msg_name;
			$arr_return["name"]="复件 ".$msg_name;
		}
		else{
			$ii="(".$i.") ";
			$str_topath=$str_todir."复件 ".$ii.$msg_name;
			$arr_return["name"]="复件 ".$ii.$msg_name;
		}
		$i++;
	}
	dir_create($str_topath);
	$arr_list = glob($str_fromdir.'*');
	$str_topath.="/";
	foreach($arr_list as $item)
	{
		$str_path = $str_topath.basename($item);
		if(file_exists($str_path) && !is_writable($str_path)) dir_chmod($str_path);
		if(is_dir($item))
		{
			dir_copy($item,basename($item),$str_topath);
		}
		else
		{
			copy($item, $str_path);
			chmod($str_path, 0777);
		}
	}
	return $arr_return;
}
function dir_iconv($in_charset, $out_charset, $dir, $fileexts = 'php|html|htm|shtml|shtm|js|txt|xml')
{
	if($in_charset == $out_charset) return false;
	$arr_list = get_dir_list($dir);
	foreach($arr_list as $item)
	{
		if(preg_match("/\.($fileexts)/i", $item) && is_file($item))
		{
			file_put_contents($item, iconv($in_charset, $out_charset, file_get_contents($item)));
		}
	}
	return true;
}
function dir_rename($msg_oldname,$msg_newname)
{
	$return_msg="";
	$str_oldname=$msg_oldname;
	$str_newname=$msg_newname;
	if(!is_dir($str_oldname))return "原目录不存在！";
	if(is_dir($str_newname))return "新目录名已经存在！";
	if(!rename($str_oldname,$str_newname)){
		$return_msg="重命名目录失败！";
	}
	return $return_msg;
}
function dir_touch($msg_path, $msg_mtime = TIME, $msg_atime = TIME)
{
	if(!is_dir($msg_path)) return false;
	$str_path = get_real_path($msg_path);
	touch($str_path, $msg_mtime, $msg_atime);
	$arr_files = glob($str_path.'*');
	foreach($arr_files as $item)
	{
		is_dir($item) ? dir_touch($item) : touch($item, $msg_mtime, $msg_atime);
	}
	return;
}


function dir_chmod($msg_dir, $msg_mode = 777, $msg_require = 0)
{
	$return_msg="";
	if(strlen($msg_mode) == 3) $msg_mode = '0'.$msg_mode;
	@chmod($msg_dir, $msg_mode);
	if($msg_require)
	{
		$arr_files = get_dir_list($msg_dir);
		foreach($arr_files as $item)
		{
			@chmod($item, $msg_mode);
		}
	}
	return $return_msg;
}

function dir_delete($msg_dir)
{
	$str_dir = $msg_dir;
	if(!is_dir($str_dir)) return "目录不存在！";
	if(substr($str_dir, 0, 1) == '.') return "不能删除目录($str_dir) !";
	if(substr($str_dir,-1)!="/") $str_dir=$str_dir."/";
	$arr_list = glob($str_dir.'*');
	foreach($arr_list as $item)
	{
		is_dir($item) ? dir_delete($item) : @unlink($item);
	}
	$msg_return=@rmdir($str_dir);
	return $msg_return;
}
function file_copy($msg_frompath,$msg_name,$msg_todir,$msg_isover=0)
{
	$arr_return=array("err"=>"","name"=>$msg_name);
	$return_val="";
	$str_topath="";
	$str_frompath = $msg_frompath;
	$str_todir = $msg_todir;
	if(!is_dir($str_todir)) dir_create($str_todir);
	if(substr($str_todir,-1)!="/") $str_todir=$str_todir."/";
	$str_topath=$str_todir.$msg_name;
	$i=0;
	$ii="";
	if(!file_exists($str_frompath)){
		$arr_return["err"]="原文件不存在!";
		return $arr_return;
	}
	if($msg_isover==1&&file_exists($str_topath)){
		$arr_return["err"]=$str_todir."目录下面已存在".$msg_name."文件!";
		return $arr_return;
	}else{
		while(file_exists($str_topath))
		{
			if($i==0)
			{
				$str_topath=$str_todir."复件 ".$msg_name;
				$arr_return["name"]="复件 ".$msg_name;
			}
			else{
				$ii="(".$i.") ";
				$str_topath=$str_todir."复件 ".$ii.$msg_name;
				$arr_return["name"]="复件 ".$ii.$msg_name;
			}
			$i++;
		}
	}
	$lng_return=@copy($str_frompath, $str_topath);
	if($lng_return==1){
		$arr_return["err"]="";
	}else{
		$arr_return["err"]="复制文件错误，编号[".$lng_return."]";
	}
	return $arr_return;
}

function file_rename($msg_oldname,$msg_newname)
{
	$return_msg="";
	$str_oldname=$msg_oldname;
	$str_newname=$msg_newname;
	if(!file_exists($str_oldname))return "原文件不存在！";
	if(file_exists($str_newname))return "新文件名已经存在！";
	if(!@rename($str_oldname,$str_newname)){
		$return_msg="重命名文件失败！";
	}
	return $return_msg;
}
function file_create($msg_path,$msg_content,$msgtype=0)
{
	$arr_return=array("err"=>"");
	$str_path=$msg_path;
	$str_dir=dirname($msg_path);
	if($msgtype==1){
		//目录不存在，则创建
		dir_create($str_dir);
	}
	//$obj_file=fopen($str_path,"w");
	//fwrite($obj_file,$msg_content);
	//fclose($obj_file);
	file_put_contents($str_path,$msg_content);
	return $arr_return;
}

function file_delete($msg_file)
{
	$str_file = $msg_file;
	if(!is_file($str_file)) return "文件不存在！";
	@unlink($str_file);
	return "";
}


function get_dir_list($msg_path, $msg_exts = '', $msg_list= array())
{
	$str_path = $msg_path;
	$arr_files = glob($str_path.'*');
	$arr_list=$msg_list;
	foreach($arr_files as $item)
	{
		$arr_list[] = $item;
		if(is_dir($item))
		{
			$arr_list = get_dir_list($item, $msg_exts, $arr_list);
		}
	}
	return $arr_list;
}
function get_dirs($msg_path)
{
	$str_path=$msg_path;
	if(!is_dir($str_path)) return "";
	if(substr($str_path,-1,1)!="/") $str_path=$str_path."/";
	$arr_items=array();
	$arr_x=array();
	if (is_dir($str_path)) 
	{
		if ($obj_dir = opendir($str_path))
		{
			   while (($str_file = readdir($obj_dir)))
			   {
				 if(filetype($str_path.$str_file)=="dir"&&$str_file!="."&&$str_file!=".."){
					 $arr_x=get_dir_perms($str_path.$str_file);
					 $arr_items[]=$arr_x;
				 }
			   }
			   closedir($obj_dir);
		}
	}
	return $arr_items;
}
function get_files($msg_path)
{
	$str_path=$msg_path;
	if(substr($str_path,-1,1)!="/") $str_path=$str_path."/";
	if(!is_dir($str_path)) return "";
	$arr_items=array();
	if ($obj_dir = opendir($str_path))
	{
		   while (($str_file = readdir($obj_dir)) !== false)
		   {
			 if(filetype($str_path.$str_file)=="file"){
				 $arr_items[]=get_file_perms($str_path.$str_file);
			 }
		   }
		   closedir($obj_dir);
	}
	return $arr_items;
}
function get_file_perms($msg_path)
{
	if(!is_file($msg_path)) return "";
	$str_path=$msg_path;
	$arr_file=array();
	$arr_file["name"]=basename($str_path);
	$arr_file["size"]=get_file_size($str_path);
	$arr_file["atime"]=date("y-m-d H:i:s",fileatime($str_path));
	$arr_file["ctime"]=date("y-m-d H:i:s",filectime($str_path));
	$arr_file["mtime"]=date("y-m-d H:i:s",filemtime($str_path));
	$arr_file["type"]=get_file_ext($str_path);
	$arr_file["ext"]=get_file_ext($str_path);
	return $arr_file;
}
function get_file_ext($msg_filename) 
{ 
	//$str_extend = pathinfo($msg_filename); 
	//@$str_extend = strtolower($str_extend["extension"]); 
	$arr_x=explode(".",$msg_filename);
	$lng_x=count($arr_x);
	if($lng_x>0){
		$str_extend=$arr_x[$lng_x-1];
	}else{
		$str_extend="";
	}
	return $str_extend; 
} 
function get_file_name($msg_file,$msg_ext="")
{
	$str_name=basename($msg_file);
	if($msg_ext==""){
		return $str_name;
	}else{
		$str_name=substr($str_name,0,-(strlen($msg_ext)+1));
		return $str_name;
	}
}
function get_dir_perms($msg_path)
{
	$arr_dir=array();
	$msg_path=format_path($msg_path);
	if(!is_dir($msg_path)) return $arr_dir;
	$arr_path=explode("/",$msg_path);
	$lng_i=count($arr_path)-1;
	$arr_dir["name"]=$arr_path[$lng_i];
	$arr_dir["atime"]=date("y-m-d H:i:s",fileatime($msg_path));
	$arr_dir["ctime"]=date("y-m-d H:i:s",filectime($msg_path));
	$arr_dir["mtime"]=date("y-m-d H:i:s",filemtime($msg_path));
	$arr_dir["type"]="文件夹";
	return $arr_dir;
}
function get_file_size($msg_path,$msg_flag="K")
{
	$int_bytes=filesize($msg_path);
	$str_flag=strtoupper($msg_flag);
	switch($str_flag)
	{
		case 'K':
			$int_size=round($int_bytes/1024,2);
			break;
		case "M":
			$int_size=round($int_bytes/(1024*1024),2);
			break;
		case "G":
			$int_size=round($int_bytes/(1024*1024*1024),2);
			break;
	}
	($int_bytes>0&&$int_size<1)?$int_size=1:"";
	return $int_size.$str_flag;

}

function upload($msg_file,$msg_path)
{
	$str_file=$msg_file;
	$str_path=$msg_path;
	move_uploaded_file($str_file,$str_path);
}


?>