<?php
/*
	[SueamCMS] (C)2009-2010 ZMRNet Studio
	mgt_file.php by 2009-10-16 zmrnet
*/
require_once(dirname(__FILE__).'/include/manage.php');

error_reporting(0);

/* 上传文件 */
function upfile($file_var,$tofile,$filepath){
	/* 参数说明:
	
	*/
	if(!is_writable($filepath)){
		echo $filepath.' 目录不存在或不可写';
		return false;
		exit;
	}
	//echo $_FILES["$file_var"]['name'];
	$Filetype=substr(strrchr($_FILES["$file_var"]['name'],"."),1); 
	($tofile==='')?($uploadfile = $_FILES["$file_var"]['name']):($uploadfile = $tofile.".".$Filetype);//文件名
     $Array[tofile] = $tofile.'.'.$Filetype;
	 $Array[oldfile]= $_FILES["$file_var"]['name'];
	if(!($uploadfile==='')){
		if (!is_uploaded_file($_FILES["$file_var"]['tmp_name'])){
             echo $_FILES["$file_var"]['tmp_name']." 上传失败.";
			 return false;
             exit;
		}

	    if (!move_uploaded_file($_FILES["$file_var"]['tmp_name'],$filepath.'/'.$uploadfile)){
             echo "上传失败。错误信息:\n";
             print_r($_FILES);
             exit;
			 }else{
			 return $Array;
			 }
	}else{
		return false;
	echo"无法上传";
	}
}
/* 获取文件大小 */
function getSize(&$fs){
	if($fs<1024){
		return $fs.'Byte';
	}elseif($fs>=1024&&$fs<1024*1024){
		return @number_format($fs/1024, 3).' KB';
	}elseif($fs>=1024*1024 && $fs<1024*1024*1024){
		return @number_format($fs/1024/1024, 3).' M';
	}elseif($fs>=1024*1024*1024){
		return @number_format($fs/1024/1024/1024, 3).' G';
	}
}
// 下载文件
if ($_GET['downfile']){
	$downfile=$_GET['downfile'];
	if (!@is_file($downfile)) {
		header('Content-type: text/html; charset=utf-8');
		echo '<script>alert(\'你要下的文件不存在\')</script>';
	}
	header('Content-Type: application/octet-stream');
	header('Content-Disposition: attachment; filename='.basename($downfile));
	header('Content-Length: '.filesize($downfile));
	@readfile($downfile);
	exit(0);
}

// 删除文件
if(@$_GET['delfile']!=''){
	$delfile=$_GET['delfile'];
	header('Content-type: text/html; charset=utf-8');
	if(file_exists($delfile)) {
		@unlink($delfile);
	} else {
		$exists='1';
		echo "<script>alert(\"文件已不存在\")</script>";
	}
	if(!file_exists($delfile)&&$exists!="1") {
		echo"<script>alert(\"删除成功\")</script>";
	} else {
		echo"<script>alert(\"删除失败\")</script>";
	}
}
/* 检测当前目录值 */
$cpath	= $_POST['path']?$_POST['path']:($_GET['path']?$_GET['path']:false);
if($cpath===false){
	$cpath	= dirname(__FILE__);
}
$cpath	= realpath(str_replace('\\','/',$cpath));

/* 检查完毕 */
/* 新建 目录 */
if($_POST['dirname']){
	$newdir	= $cpath.'/'.$_POST['dirname'];
	if(is_dir($newdir))
	{
		echo"<script>alert(\"此目录名已经存在!\")</script>";
		exit;
	}else {
		if(mkdir($newdir,0700))
		{
			echo"<script>alert(\"创建成功!\")</script>";
		}else {
			echo "<script>alert(\"创建失败!\")</script>";
		}
	}
}
/* 上传文件 */
if($_POST['upload']){
	if(!(upfile("upfiles",$_POST['fname'],$cpath)))
	{
		echo"<script>alert(\"上传失败!\")</script>";
	}else {
		echo "<script>alert(\"上传成功!\")</script>";
	}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><? echo $sys_title; ?></title>
<link href="images/sueam.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div style="margin:20px;">
	<div style="margin:10px;font-size:14px;color:green"><b>文件管理</b></div>
	<div class="sec_menu" style="padding:20px;">
		<div style="padding:5px;text-align:right;">
			<font color="blue">PHP版本：</font><font color="red"><?php echo PHP_VERSION; ?></font>
			&nbsp;&nbsp;&nbsp;&nbsp;
			<font color="blue">服务器：</font><font color="red"><?php echo php_uname(); ?></font>
		</div>
		<div style="padding:5px;text-align:left;">
			<form name="form1" method="post" action="">
				目录选择：<input name="path" type="text" class="input_txt">
				<input type="submit" name="cpath" value="跳 转" class="input_btn">
			</form>
			<form name="form2" method="post" action="">
				新建目录：<input name="dirname" type="text" class="input_txt">
				<input type="submit" name="mkdir" value="建 立" class="input_btn">
			</form>
			<form name="form3" method="post" action="" enctype="multipart/form-data">
				上传文件：<input name="upfiles" type="file" class="input_txt3"><br>
				新文件名：<input name="fname" type="text" class="input_txt">
				<input type="submit" name="upload" value="上 传" class="input_btn">
			</form>
			<font color="blue">当前路径：</font>
			<font color="red"><?php echo iconv('GB2312', 'UTF-8', $cpath); ?></font>
		</div>
		<div style="padding:5px;text-align:left;">
			<strong>子目录</strong><br>
<?php
$dh = @opendir($cpath);
while($file=@readdir($dh)){
	$path = $cpath.'/'.$file;
	if(@is_dir($path)){
		$fn = iconv('GB2312', 'UTF-8', $file);
		if($file!='..'&&$file!='.')	{
			echo '			【目录】';
			echo '<a href="?path='.urlencode($cpath).'/'.urlencode($file).'">'.$fn.'</a><br>'."\n";
		}else if($file=='..'){
			echo '			【上级】';
			echo '<a href="?path='.urlencode($cpath).'/'.urlencode($file).'">上级目录</a><br>'."\n";
		}
	}
}
@closedir($dh); 
?>
		</div>
		<div style="padding:5px;text-align:left;">
			<strong>文件列表</strong><br>
			<font color="red">[</font>&nbsp;
			<font color="blue">文件操作</font>&nbsp;
			<font color="red">]</font>&nbsp;
		    ◇ 文件名&nbsp;&nbsp;
			<font color="green">(</font>
			<font color="blue">修改日期</font>
			<font color="red">文件大小</font>
			<font color="green">)</font>
		</div>
		<div style="padding:5px;text-align:left;">
<?php
$dh = @opendir($cpath);
while($file=@readdir($dh)){
	$path = $cpath.'/'.$file;
	if(@is_dir($path)=='0'){
		$fn = iconv('GB2312', 'UTF-8', $file);
		$size = @filesize($path);
		$size = @getSize($size);
		$time = @date('Y-n-d H:i:s',filemtime($path));
?>
			<font color="red">[</font>
			<a href="?downfile=<? echo urlencode($cpath); ?>/<? echo urlencode($file); ?>">下载</a> 
			<a href="?path=<? echo urlencode($cpath); ?>&delfile=<? echo urlencode($cpath); ?>/<? echo urlencode($file); ?>">删除</a>
			<font color="red">]</font>
			<? echo '◇ '.$fn; ?>&nbsp;&nbsp;
			<font color="green">(</font>
			<font color="blue"><? echo $time; ?></font>
			<font color="red"><? echo $size; ?></font>
			<font color="green">)</font><br>
<?php
	}
}
@closedir($dh); 
?>
		</div>
	</div>
</div><br>
</body>
</html>