<?
/*
ID设计 PHP文件管理器v1.4版 
作者：咖啡（coffee) 
主页:http://idlerdesign.yeah.net
欢迎各位PHP爱好者对本程式进行优化 和改进。但请保留页面中原作者相关文字。
在此感谢落伍者论坛(http://im286.com) 在那里学的不少东西。
*/
?>
<? $pagestartime=microtime();?>
<?
if ($downfile) {
		if (!@is_file($downfile))
			echo"你要下的文件不存在";
        $filename = basename($downfile);
		$filename_info = explode('.', $filename);
		$fileext = $filename_info[count($filename_info)-1];
		header('Content-type: application/x-'.$fileext);
		header('Content-Disposition: attachment; filename='.$filename);
		header('Content-Description: PHP3 Generated Data');
		readfile($downfile);
		exit;
	}
?>


<?php
if($dir=="")
$dir="./";
?>

<html> 
<head>
<title>ID PHP文件管理器 Powrerd by 咖啡</title>
<style type="text/css">
<!--
.forn {  font-family: "宋体"; font-size: 9pt; line-height: 13pt; color: #000000; text-decoration: none}
-->
</style>
</head> 
<body bgcolor="#FFFFFF" leftmargin="5">
<center>

<?
if ($editfile!=""&&$action!='copy'&&$n=='1')
{
  $filename = "$editfile";
  $fd = fopen( $filename, "r" );
  $contents = fread($fd, filesize($filename));
  fclose( $fd );
  $contents= str_replace("</textarea>","<//textarea>",$contents);
  $contents= str_replace("\\","\\\\",$contents);
  echo"
  <form name='edit' method='post' action=''>
   <font size='2'> 目前正在编辑文件名:</font> 
    <input type='text' name='editfilename' style=\"font-size:9pt;background-color:#ffffff;border: 1px solid #500000;color:#500000\" value=$editfile>
     <font size='2'> （你可以给文件改名,前的的路径不要改) 复制:</font> 
    <input type='checkbox' name='copy' value='1'>
	<font color=red size='2'>注意事件：选中复制时，请先给复后生成的文件命个名子。并且先确定该空间中，没有该名字文件，不然将会覆盖掉.如不选复制，只改命名，作用等同于改该文件名！</font>
    <textarea name='editfiletxt' cols='110' rows='26' style=\"font-size:9pt;background-color:#ffffff;border: 1px solid #500000;color:#500000\">$contents </textarea>
    <br>
	<input type='hidden' name='action' value='copy'>
    <input  type='submit' value='提交'style=\"font-size:9pt;background-color:#ffffff;border: 1px solid #500000;color:#500000\" >
    <input type='reset' value='重置' style=\"font-size:9pt;background-color:#ffffff;border: 1px solid #500000;color:#500000\">
  </form>";
}


else
{
?>
  <form enctype="multipart/form-data" action="?dir=<? echo"$dir";?>" method="post" name=fm class="forn">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr> 
        <td class="forn">程序目录: 
          <? 
		$path="";
		$pathname=str_replace('\\','/',dirname(__FILE__));
		echo "<a href='?'>$pathname</a>";?>
          服务器IP： 
          <? echo  getenv("REMOTE_HOST");?>
          <font color="#FFFFFF"> </font><span class="forn">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#FF0000"> 
          I</font><font color="#0000FF">D</font><font color="#9900CC">设计</font> 
          <font color="#666666">PHP文件管理器 v1.4</font></span></td>
      </tr>
    </table>
    <table width="100%" border="0" cellspacing="1" cellpadding="2" bgcolor="#000000" class="forn">
      <tr bgcolor="#FFFFFF"> 
        <td width="14%" align="center" bgcolor="#CCCCCC"><font size="2">请选择文件： 
          </font></td>
        <td width="59%"><font size="2"> 
          <input name="userfile" type="file" style="font-size:9pt;background-color:#ffffff;border: 1px solid #500000;color:#500000"  >
          </font></td>
        <td rowspan="5" align="left" valign="top" width="27%"> 
          <?
if(!$userfile&&!$delfile&&!$deldir&&!$mkdir&&!$editfile)
{
echo"PHP探针：<br>";
echo"<font color=red>服务器PHP版本:</font>php";
echo PHP_VERSION."<br>";
echo"<font color=red>服务器操作系统:</font>";
echo PHP_OS."<br>";
echo"<font color=red>服务器解译引擎:</font>";
echo getenv("SERVER_SOFTWARE")."<br>";
echo"<font color=red>脚本超时时间:</font>";
echo get_cfg_var("max_execution_time")."秒<br>";
echo"<font color=red>服务器端口:</font>";
echo  getenv("SERVER_PORT");
echo"<font color=red> 空间大小:</font>";
echo intval(diskfreespace(".") / (1024 * 1024)).'Mb<br>';
echo"<font color=red> 可上传最大单文件:</font>";
echo get_cfg_var("upload_max_filesize");

}		?>
          <? 
if(@$userfile!="") 
{ 
@$v=file_exists($filedir); 
if(!$v) 
{ 
mkdir(@$filedir,0777); 
} 
@chmod($filedir,0777);
if(@$lastname!="")
{
$lastname=$lastname;
}
else 
$lastname=$userfile_name;
if(file_exists("$filedir/$lastname")=="1")
echo"同名文件存在，请换个名子上传";
else
{
copy($userfile,"$filedir/$lastname"); 
echo $userfile_name."--该文件的原始名称<br>"; 
echo $lastname."上传后的名称<br>";
echo $userfile_size."--上传文件的实际字节数<br>"; 
echo $userfile_type."-文件类型"; 
echo "<p>〖<a href='$filedir/$userfile_name' target='_blank'>看看</a>〗"; 
}
echo "〖<a href='javascript:history.back(1)'>返回</a>〗"; 

} 
?>
          <?php
if($delfile!="")
{
if(file_exists($delfile))
{
chmod("$delfile",0777);
unlink($delfile);
}
else{
$xx="1";
echo "文件已不存在<br>";
 }
if(!file_exists($delfile)&&$xx!="1")
echo"删除成功";
else
echo"删除失败";
}
?>
          <?php
if($deldir!="")
{
  $deldirs="$dir/$deldir";
  if(file_exists("$deldirs")) //是否为空目录检测
  {
    $mydir=dir($deldirs);
    while($files=$mydir->read())
	{
    $i=$i+1;
    if($i>2)break;
    }
	$mydir->close(); //不关闭，后面就不能删除了。晕。总是忘。费了好大功夫才找出来
  }
  if(file_exists("$deldirs")&&$i==2)
  {
  chmod("$deldirs",0777);
  rmdir("$deldirs");
  $xy="1";
  }
  if($i>2)
  echo"<font color=red>此目录里面有文件，如果要将整个目录全部删除，请点---> <a href='?dir=$dir&deldir=$deldir&deltree=$deldir'>删除整个目录</a></font>";


  if(!file_exists("$deldirs"))echo"目录不存在！";
  if($xy=="1")echo"删除完必,";
}
?>
          <?
if($mkdir)
{
$mkdirs="$dir//$mkdir";
if(file_exists("$mkdirs"))
echo"该目录存在";
else
{
mkdir("$mkdirs",0777);
if(file_exists("$mkdirs"))
echo"目录创建成功";
else
echo"目录创建失败";
}
}

?>
          <?
if($deltree!=""&&$deldir!="")
{
if($deltree==$deldir)
{
$deldirs="$dir/$deltree";
if(file_exists("$deldirs"))
{
deltree($deldirs);
echo"删除整个目录完必!<br>";
}else
echo"文件已不存在!<br>";
}
else
echo"错误操作！！<a href='javascript:history.back(1)'>反回</a>";
}

?>
          <?
//编辑文件部分
if ($editfile!="")
{
$filename=$editfilename;

if($copy!="1"){
if(file_exists($editfile))
{
chmod("$editfile",0777);
unlink($editfile);}
}
if($copy=="1"&&$editfile==$editfilename)
{
echo"您要复制？可没有给新的文件改个名字，现在两个文件名相同，请<a href='javascript:history.back(1)'>返回</a>";
}
else{
$editfiletxt= str_replace("||||","〓〓",$editfiletxt);
$editfiletxt= str_replace("\\\\","||",$editfiletxt);
$editfiletxt= str_replace("\\","",$editfiletxt);
$editfiletxt= str_replace("||||","\\",$editfiletxt);
$editfiletxt= str_replace("〓〓","||||",$editfiletxt);
$editfiletxt= str_replace("<//textarea>","</textarea>",$editfiletxt);
writetofile("$filename","$editfiletxt");
if(file_exists($filename))echo"写入成功";
elseif(!file_exists($filename))
echo"失败";
}}
?>
          
        </td>
      </tr>
      <tr bgcolor="#FFFFFF"> 
        <td width="14%" align="center" bgcolor="#CCCCCC"><font size="2">文件上传的目录: 
          </font></td>
        <td width="59%"><font size="2"> 
          <input type="text" name="filedir"  style="font-size:9pt;background-color:#ffffff;border: 1px solid #500000;color:#500000" value="<? echo"$dir";?>">
          请用服务器上相对目路径</font></td>
      </tr>
      <tr bgcolor="#FFFFFF"> 
        <td width="14%" align="center" height="3" bgcolor="#CCCCCC"><font size="2">上传后的文件名:</font> 
        </td>
        <td width="59%" height="3"> 
          <input type="text" name="lastname" style="font-size:9pt;background-color:#ffffff;border: 1px solid #500000;color:#500000">
          可不写 <font size="2">(注意，请在后面加后缀名！如：name.zip）</font></td>
      </tr>
      <tr bgcolor="#FFFFFF"> 
        <td width="14%" bgcolor="#CCCCCC">&nbsp;</td>
        <td width="59%"> 
          <input type="submit" value="上传" style="font-size:9pt;background-color:#ffffff;border: 1px solid #500000;color:#500000" >
          <input type="reset" name="Submit" value="重置" style="font-size:9pt;background-color:#ffffff;border: 1px solid #500000;color:#500000">
        </td>
      </tr>
      <tr bgcolor="#FFFFFF"> 
        <td colspan="2" bgcolor="#CCCCCC" height="2">还有很多功能我正在想，尽量将这个文件功能变的更强大.请提出建议 
          mail:<a href="mailto:phpcoffee@hotmail.com%20">phpcoffee@hotmail.com 
          </a></td>
      </tr>
    </table>
</form> 
<form name="form1" method="get" action="" class="forn">
  <span class="forn">当前路径: 
  <? echo $NowDir = getMyPath($pathname, $dir);?>
  </span> 磁盘转换: 
  <input type="text" name="dir" size="5"style="font-size:9pt;background-color:#ffffff;border: 1px solid #500000;color:#500000" >
    <input type="submit" value="转换" style="font-size:9pt;background-color:#ffffff;border: 1px solid #500000;color:#500000">
  输入如：c:,d: 一类的盘符，如你的权限够。下面将会显示该盘内容 
</form>
<span class="forn"> </span> 
  <table width="100%" border="0" cellspacing="1" cellpadding="2" class="forn" bgcolor="#000000" height="44">
    <tr bgcolor="#CCCCCC"> 
      <form name="form2" method="post" action=""> 
        <td width="29%" height="4"> 目录: 
          <input type="text" name="mkdir" size="15" style="font-size:9pt;background-color:#ffffff;border: 1px solid #500000;color:#500000" >
          <input type="submit" value="创建" style="font-size:9pt;background-color:#ffffff;border: 1px solid #500000;color:#500000" >
        </td>
      </form>
      <td width="71%" height="4">文件：</td>
  </tr>
  <tr bgcolor="#FFFFFF"> 
    <td width="29%" align="left" valign="top" height="9"> 
      <?php
$dirs=opendir($dir);
while ($file=readdir($dirs)) {
     $b="$dir/$file";
     $a=is_dir($b);
	if($a=="1"){
	if($file!=".."&&$file!=".")
    echo "<table width='100%' border='0' cellspacing='0' cellpadding='0' class='forn' onMouseOut=\"this.bgColor='#ffffff';\" 
                onMouseOver=\"this.bgColor='#cccccc';\"><tr> 
          <td width='80%'>□ <a href='?dir=$dir/$file'>$file</a></td>
          <td width='20%'><a href='?dir=$dir&deldir=$file'>删除</a></td></tr></table>";
     else
	 {
	 if($file=="..")
	 echo "↑<a href='?dir=$dir/$file'> 上级目录</a>";
	 }
	}
	}
closedir($dirs); 
?>
    </td>
    <td width="71%" valign="top" align="left" height="9"> 
      <table width="100%" border="0" cellspacing="0" cellpadding="0" class="forn">
        <tr align="center"> 
          <td width="34%">名称</td>
          <td width="23%">时期</td>
          <td width="25%">大小</td>
          <td width="18%">操作</td>
        </tr>
      </table>
      <?php
$dirs=opendir($dir);
while ($file=readdir($dirs)) {
     $b="$dir/$file";
     $a=is_dir($b);
	if($a=="0"){
	$size=filesize("$dir/$file");
	$lastsave=date("Y-n-d H:i",filectime("$dir/$file"));
    echo "
	<table width='100%' border='0' cellspacing='0' cellpadding='0' class='forn' onMouseOut=\"this.bgColor='#ffffff';\" 
                onMouseOver=\"this.bgColor='#cccccc';\" >
        <tr>
          <td width='35%'><a href=$dir/$file target=_blank>$file</a> </td>
		   <td width='24%'>$lastsave</td>
          <td width='24%'>$size 字节</td>
          <td width='6%'><a href='?downfile=$dir/$file' target='_blank'>下载</a></td>
          <td width='6%'><a href='?editfile=$dir/$file&n=1'>编辑</a></td>
		  <td width='6%'><a href='?dir=$dir&delfile=$dir/$file'>删除</a></td>
		</tr>
      </table>";
	}
	}
closedir($dirs); 
?>
    </td>
  </tr>
</table>

<?
}?>
  <span class="forn"><font color="#FF0000">声明:</font>由本程序造成的一切后果，与本人无关<br>
  Powered by <a href="http://idlerdesign.yeah.net" target="_blank">咖啡</a> <br>
  转载时请保留这些信息<br> 
  <?php
$pageendtime=microtime();
$starttime=explode(" ",$pagestartime);
$endtime=explode(" ",$pageendtime);
$totaltime=$endtime[0]-$starttime[0]+$endtime[1]-$starttime[1];
$timecost=sprintf("%s",$totaltime);
echo "<font color=red>页面运行时间：$timecost 秒 </font>";?></span>
</center>
</body> 
</html>

<?

function writetofile($filename,$book)
{
$fd=fopen("$filename","w");
fwrite($fd,$book);
fclose($fd);
}


function deltree($TagDir) //整目录及文件删除 
{ 
$mydir=dir($TagDir); 
while($file=$mydir->read()){ 
if((is_dir("$TagDir/$file")) AND ($file!=".") AND ($file!="..")) 
{ 
deltree("$TagDir/$file"); 
} 
else 
@unlink("$TagDir/$file"); 

} 
$mydir->close(); 
@rmdir($TagDir); 
} 




	function getMyPath($mainpath, $relativepath)
	{
		global $dir;

		$mainpath_info           = explode('/', $mainpath);
		$relativepath_info       = explode('/', $relativepath);
		$relativepath_info_count = count($relativepath_info);
		for ($i=0; $i<$relativepath_info_count; $i++) {
			if ($relativepath_info[$i] == '.' || $relativepath_info[$i] == '') continue;
			if ($relativepath_info[$i] == '..') {
				$mainpath_info_count = count($mainpath_info);
				unset($mainpath_info[$mainpath_info_count-1]);
				continue;
			}
			$mainpath_info[count($mainpath_info)] = $relativepath_info[$i];
		} //end for

		return implode('/', $mainpath_info);
	}

?>
