PHP文件管理函数(无样式)

该函数对小型文件仓库管理,还是够用的,可当文件管理整合在自己编写的网站系统里,样式无加载,需要自行设计样式。
仅供参考。
代码如下:

<?php
/**
 * by hkshadow 
 * 2011-10-03
 **/
$s = isset($_GET['s']) ? $_GET['s'] : "";//测定变量是否设定
$p = isset($_GET['p']) ? $_GET['p'] : "";
$f = isset($_GET['f']) ? $_GET['f'] : "";
$fpath = isset($_GET['path']) ? $_GET['path'] : "";
$path=str_replace('\\','/',dirname(__FILE__)).'/';
if($fpath!=""){!$path && $path = '.';$paths=str_replace('//','/',$_GET['path']);$path1=str_replace('//','/',opath($path,$paths));ofile($path1);}
switch($s){//函数调用
  case "main": ofile($path);break;
  case "redir": redir($p);break;
  case "refile": refile($p);break;
  case "upload": upload($p);break;
  case "edit": edit($p,$f);break;
  case "del": del($p,$f,$_GET['i']);break;
  case "perms": perms($p,$f);break;
  case "ref": ref($p,$f);break;
  case "cfile": cfile($p,$f);break;
  case "deldir": deldir($p,$f);break;
  default: break;
}
//文件目录列表
function ofile($path){
$pathw=is_writable($path) ? "可写" : "不可写";
echo '<form method="GET">';
echo ' [<a href="?s=redir&p='.urlencode($path).'">创建目录</a> | ';
echo ' <a href="?s=refile&p='.urlencode($path).'">创建文件</a> | ';
echo ' <a href="?s=upload&p='.urlencode($path).'">上传文件</a></a>]';
echo '</a><br>当前路径('."$pathw".'): <input type="text" name="path" value="'."$path".'" size="68"> ';
echo '<input type="submit" value="Enter"></form>';
echo '<table bgcolor="#000000" cellpadding=1 cellspacing=1 width="850"><tr align="center">';
echo '<td width="430" bgcolor="#6959CD">名称</td>';
echo '<td width="50" bgcolor="#6959CD">下载</td>';
echo '<td width="120" bgcolor="#6959CD">大小</td>';
echo '<td width="250" bgcolor="#6959CD">操作</td>';
echo '</tr>';
$spider=@opendir($path);
while (false !== ($file=@readdir($spider))){
	$filebug = strlen($file)>50 ? substr($file,0,50) : $file;
	$filedir="$path"."$file";
	$dir=@is_dir($filedir);
	if($dir=="1"){
		if($file!="."){
			if($file==".."){
				echo '<tr><td width="430" bgcolor="#AAAAAA">';
				echo '<a href="?path='.urlencode(uppath($filedir)).'"><b>[返回上级目录]</b></a></td>';
				echo '<td width="50" bgcolor="#AAAAAA"><div align="center">===</div></td>';
				echo '<td width="120" bgcolor="#AAAAAA"><div align="center">===</div></td>';
				echo '<td width="250" bgcolor="#AAAAAA"><div align="center">===</div></td></tr>'."\n";
			}
			else{
				$dirw=is_writable($filedir) ? "可写" : "不可写";
				echo '<tr><td width="430" bgcolor="#AAAAAA">';
				echo '<font face="wingdings" size="3">1</font><a href="?path='.urlencode($filedir).'">'."$filebug".'</a>';
				echo '<td width="50" bgcolor="#AAAAAA"><div align="center">'."$dirw".'</div></td>';
				echo '<td width="120" bgcolor="#AAAAAA"><div align="center">dir</div></td>';
				echo '<td width="250" bgcolor="#AAAAAA"><div align="center">';
				echo '[<a href="#" onclick="if(confirm(\'确认要删除目录'."$file".'吗?\')==true) document.location.href=\'?s=deldir&p='.urlencode($path).'&f='.urlencode($file).'\';">删除</a>] ';
				echo '[<a href="?s=perms&p='.urlencode($path).'&f='.urlencode($file).'">属性</a>] ';
				echo '[<a href="?s=ref&p='.urlencode($path).'&f='.urlencode($file).'">改名</a>]</div></td></tr>'."\n";
			}
		}
	}
}
@closedir($spider);
$spider=@opendir($path);
while (false !== ($file=@readdir($spider))){
	$filebug = strlen($file)>50 ? substr($file,0,50) : $file;
	$filedir="$path"."$file";
	$dir=@is_dir($filedir);
	$pathg=str_replace('\\', '/', dirname(__FILE__)).'/';
	$fileurl = str_replace($pathg,'',$path);
	if($dir=="0"){
		if($file!='..'){
		$fsize=@filesize($filedir);
		$fsize=$fsize/1024;
		$fsize=@number_format($fsize, 3);
			echo '<tr><td width="430" bgcolor="#AAAAAA">';
			echo '<font face="wingdings" size="3">4</font> <a target="_blank" href="./'."$fileurl"."$file".'">'."$filebug".'</a></td>';
			echo '<td width="50" bgcolor="#AAAAAA"><div align="center"><a href="?fd='.urlencode($filedir).'">下载</a></td>';
			echo '<td width="120" bgcolor="#AAAAAA"><div align="center">'."$fsize".' KB</td>';
			echo '<td width="250" bgcolor="#AAAAAA"><div align="center">';
			echo '[<a href="?s=edit&p='.urlencode($path).'&f='.urlencode($file).'">编辑</a>] ';
			echo '[<a href="#" onclick="if(confirm(\'确认要删除文件'."$file".'吗?\')==true) document.location.href=\'?s=del&p='.urlencode($path).'&f='.urlencode($file).'\';">删除</a>] ';
			echo '[<a href="?s=perms&p='.urlencode($path).'&f='.urlencode($file).'">属性</a>] ';
			echo '[<a href="?s=ref&p='.urlencode($path).'&f='.urlencode($file).'">改名</a>] [<a href="?s=cfile&p='.urlencode($path).'&f='.urlencode($file).'">拷贝</a>]</td></tr>'."\n";
		}
	}
}
@closedir($spider);
echo '</table>';
}
//返回
function back(){
	echo '<br><a href="javascript:history.go(-1);">[返回]</a><br><br>';
}
function fanhui($p){
	echo '<br><a href="?path='.urlencode($p).'">[返回]</a><br><br>';
}
//处理路径
function opath($scriptpath,$nowpath) {
	if ($nowpath == '.') {
		$nowpath = $scriptpath;
	}
	$nowpath = str_replace('\\', '/', $nowpath);
	$nowpath = str_replace('//', '/', $nowpath);
	if (substr($nowpath, -1) != '/') {
		$nowpath = $nowpath.'/';
	}
	return $nowpath;
}
function uppath($nowpath) {
	$pathdb = explode('/', $nowpath);
	$num = count($pathdb);
	if ($num > 2) {
		unset($pathdb[$num-1],$pathdb[$num-2]);
	}
	$uppath = implode('/', $pathdb).'/';
	$uppath = str_replace('//', '/', $uppath);
	return $uppath;
}
//创建目录
function redir($repath){
echo "<br>创建到当前路径: $repath <br>";
echo '<form method="POST" >目录名称:<input type="text" name="redir"><input type="submit" name="subdir" value="确定"></form>';
fanhui($repath);
	if($_POST['subdir']){
		$redir=$_POST['redir'];
		$repathok="$repath"."$redir";
		if(stristr($redir,'/')){
			echo '<br>目录名错误<br><br>';
		}
		elseif(file_exists($repathok)){
			echo '<br>目录'."$redir".'已存在<br><br>';
		}
		else{
			$msg=@mkdir($repathok,0777) ? '<br>目录'."$redir".'创建成功<br><br>' : '<br>目录'."$redir".'创建失败<br><br>';
			echo "$msg";
		}
	}
}
//创建文件
function refile($ref){
echo '<form method="POST" >路径文件名: <input type="text" name="refile" size="65" value="'."$ref".'spider.php"><br>';
echo '<textarea name="text" COLS="75" ROWS="18" >请输入内容</textarea><br>';
echo '<input type="submit" name="subfile" value="保存"></form>';	fanhui($ref);
	if($_POST['subfile']){
		$refile=$_POST['refile'];	$text=$_POST['text'];
		$fp=@fopen($refile,'w');
		if($fp==null){
			echo '<br>创建文件失败!<br><br>';
		}
		else{
		@fwrite($fp,stripslashes($text));
		@fclose($fp);
		echo "<script>window.alert('创建文件"."$refile"."成功');history.go(-1);</script>";
		}
	}
}
//上传文件
function upload($pathname){
	echo '<br>允许最大单个上传文件:'.@get_cfg_var('upload_max_filesize');
	echo '<br><div align="center"><form method="POST" enctype="multipart/form-data">';
	echo '本地文件: <input name="upfile" type="file" size="30"> <input type="submit" name="upok" value="上传"><br>';
	echo '上传到路径: <input name="up" type="text" value="'."$pathname".'" size="45"></form>';	fanhui($pathname);
	if($_POST['upok']){
		$uppath=$_POST['up'];
		echo @copy($_FILES['upfile']['tmp_name'],"".$uppath."/".$_FILES['upfile']['name']."") ? "上传文件{$_FILES['upfile']['name']}成功!<br><br>" : "上传文件{$_FILES['upfile']['name']}失败!<br><br>";
	}
}
//编辑文件
function edit($p,$f){
	$ep="$p"."$f";
	$hackfp=@fopen($ep,'r');
	$redspider=@fread($hackfp, filesize($ep));
	$redspider=htmlspecialchars($redspider);
	echo '<br>编辑文件:'."$ep";
	echo '<br><form method="POST">';
	echo '<textarea name="ftext" COLS="75" ROWS="18" >'."$redspider".'</textarea><br>';
	echo '<input type="submit" name="editok" value="保存"></form>';	back();
	if($_POST['editok']){
		$fp=@fopen($ep,'w');
		if($fp==null){
			echo '保存失败<br><br>';
		}
		else{
			$ftext=$_POST['ftext'];
			fwrite($fp,stripslashes($ftext));
			fclose($fp);
			echo "<script>window.alert('保存文件"."$ep"."成功');history.go(-1);</script>";
		}
	}
}
//删除文件
function del($dp,$df,$i){
	$delfile="$dp"."$df";
	if(!file_exists($delfile)) {
		echo '<br>文件'."$df".'不存在! ';
		if($i=="1")
		back();
		else
		fanhui($dp);
	}
	else {
		$msg = @unlink($delfile) ? '<br>文件'."$df".'删除成功!<br>' : '<br>删除失败!<br>';
		echo "$msg";
		if($i=="1")
		back();
		else
		fanhui($dp);
	}
}
//属性
function perms($pp,$pf){
	$pfile="$pp"."$pf";
	$pold=substr(base_convert(fileperms($pfile),10,8),-4);
	$ftime=@date("Y-n-d H:i:s",filectime("$pfile"));
	echo '<br>'."$pfile".' 的当前属性为: '."($pold)".' &nbsp;&nbsp;&nbsp;修改日期: '."$ftime".'<br><form method="POST">';
	echo '修改属性: <input type="text" name="pfo" value="'."$pold".'" > ';
	echo '<input type="submit" name="subperms" value="修改"></form>';	fanhui($pp);
	if($_POST['subperms']){
		$pnew=$_POST['pfo'];
		$fileperm=base_convert($pnew,8,10);
		$msg=@chmod($pfile,$fileperm)?"更改成功!":"更改失败!";
		echo "<script>window.alert('文件"."$pfile"."属性"."$msg"."');history.go(-1);</script>";
	}
}
//重命名
function ref($rp,$rf){
	$rpf="$rp"."$rf";
	echo '<br><form method="POST">路径:'."$rp".'<br>将('."$rf".') 重命名为: <input type="text" name="newf" value="'."$rf".'">';
	echo '<input type="submit" name="subref" value="修改"></form>';	fanhui($rp);
	if($_POST['subref']){
		$newf=$_POST['newf'];
		$newfile="$rp"."$newf";
		$msg = file_exists($rpf) ? (@rename($rpf,$newfile) ? "重命名为{$newf}成功" : "重命名为{$newf}失败") : "不存在";
		echo "<script>window.alert('"."$rf"."$msg"."');history.go(-1);</script>";
	}
}
//拷贝文件
function cfile($cp,$cf){
	$cpf="$cp"."$cf";
	echo '<br>文件: '."$cpf".'<br><form method="POST">拷贝到: <input type="text" name="newf" size="35" value="'."$cpf".'"> ';
	echo '<input type="submit" name="subcfile" value="确定"></form>';
	fanhui($cp);
	if($_POST['subcfile']){
		$newf=$_POST['newf'];
		if(file_exists($newf)){
			echo '失败:文件'."$newf".'已存在<br><br>';
		}
		else{
			$msg = @copy($cpf,$newf) ? '文件 '."$cpf".' 拷贝到 '."$newf".' 成功!<br><br>' : '失败,路径出错或不可写!<br><br>';
			echo "$msg";
		}
	}
}
//删除目录
function deldir($dp,$dd){
	$deldir="$dp"."$dd";
	if(isset($deldir)) {
		$msg = file_exists($deldir)?(deltree($deldir)?"删除成功":"删除失败"):"不存在";
		echo '<br><br>目录'."$dd"."$msg";	fanhui($dp);
	}
}
function deltree($dpath){
	$mydir=@opendir($dpath);
	while($file=@readdir($mydir)){
		if((is_dir("$dpath/$file")) && ($file!=".") && ($file!="..")) {
			if(!deltree("$dpath/$file")) return false;
		}else if(!is_dir("$dpath/$file")){
			@chmod("$dpath/$file", 0777);
			if(!@unlink("$dpath/$file")) return false;
		}
	}
	@closedir($mydir);
	@chmod("$dpath", 0777);
	if(!@rmdir($dpath)) return false;
	return true;
}
//页面执行时间
function pagetime(){
	global $stime;
	$paget = explode(' ', microtime());
	$ptime = number_format(($paget[1] + $paget[0] - $stime), 6);
	return $ptime;
}
function array_stripslashes(&$array) {
    while(list($key,$var) = each($array)) {
        if ((strtoupper($key) != $key || ''.intval($key) == "$key") && $key != 'argc' && $key != 'argv') {
            if (is_string($var)) $array[$key] = stripslashes($var);
            if (is_array($var)) $array[$key] = array_stripslashes($var);
        }
    }
    return $array;
}
?>