最近在研究DEDECMS,感觉它的文件管理器很不过,想想这个模块在以后的开发中可能会用到,所以就将其从系统中剥离出来,便于以后使用。
其实文件管理器涉及的内容也就是php对文件的操作,相对于其他语言来说,php来写文件管理器要容易简单的多,不想java一样,要很繁琐的操作。
先来看看它的入口文件index.php
$root=$_SERVER[DOCUMENT_ROOT];
$cmspath="$root/resource/file_manager";
$activepath=$_GET['activepath'];
if(!isset($activepath)) $activepath=$cmspath;
$inpath = "";
$activepath = str_replace("..", "", $activepath);
$activepath = preg_replace("#^\/{1,}#", "/", $activepath);
if($activepath == "/") $activepath = "";
if($activepath == "") $inpath = $cmspath;
else $inpath = $activepath;
$activeurl = $activepath;
include('view/index_view.htm');
$activepath在整个模块总一直都存在,它表示当前的文件路径,对于index来说,可以表示web根目录,也可以表示web应用的根目录,在这里可以自行进行设置
再来看加载的view/index_view.htm
文件管理器
文件名
文件大小
最后修改时间
操作
read())!==false)
{
if($file!="." && $file!=".." && !is_dir("$inpath/$file"))
{
@$filesize=filesize("$inpath/$file");
$filesize=$filesize/1024; //b->kb
$filetime=filemtime("$inpath/$file");//文件创建时间
$filetime=date("Y-m-d H:i:s",$filetime);//格式化时间
if($filesize<0.1)
{
list($ty1,$ty2)=explode(".",$filesize);
$filesize=$ty1.".".substr($ty2,0,2);
}
else
{
list($ty1,$ty2)=explode(".",$filesize);
$filesize=$ty1.".".substr($ty2,0,1);
}
}
if($file==".")
{
continue;
}
else if($file=="..")
{
if($activepath=="")
{
continue;
}
$tmp=preg_replace("#[\/][^\/]*$#i", "", $activepath);
$line="\n
上级目录
当前目录:$activepath
[图片浏览器]
";
$dirs[]=$line;
}
else if(is_dir("$inpath/$file"))
{
if(preg_match("#^_(.*)$#i" , $file)) continue;
if(preg_match("#^\.(.*)$#i" , $file)) continue;
$line = "\n
$file
[改名]
[删除]
";
$dirs[] = $line;
}
else if(preg_match("#\.(gif|png)#i",$file)) //如果是GIF或PNG
{
$line = "\n
$file
$filesize KB
$filetime
[改名]
[删除]
[移动]
";
$files[] = $line;
}
else if(preg_match("#\.(jpg)#i", $file)) //如果是jpg图片类型
{
$line = "\n
$file
$filesize KB
$filetime
[改名]
[删除]
[移动]
";
$files[] = $line;
}
else if(preg_match("#\.(swf|fla|fly)#i", $file)) //如果是flash类型
{
$line = "\n
$file
$filesize KB
$filetime
[改名]
[删除]
[移动]
";
$files[] = $line;
}
else if(preg_match("#\.(zip|rar|tar.gz)#i",$file)) //压缩文件
{
$line = "\n
$file
$filesize KB
$filetime
[改名]
[删除]
[移动]
";
$files[] = $line;
}
else if(preg_match("#\.(exe)#i", $file)) //文件类型为可执行文件
{
$line = "\n
$file
$filesize KB
$filetime
[改名]
[删除]
[移动]
";
$files[] = $line;
}
else if(preg_match("#\.(mp3|wma)#i",$file)) //音频文件
{
$line = "\n
$file
$filesize KB
$filetime
[改名]
[删除]
[移动]
";
$files[] = $line;
}
else if(preg_match("#\.(wmv|api)#i",$file)) //视频文件
{
$line = "\n
$file
$filesize KB
$filetime
[改名]
[删除]
[移动]
";
$files[] = $line;
}
else if(preg_match("#\.(rm|rmvb)#i",$file)) //视频文件rm rmvb
{
$line = "\n
$file
$filesize KB
$filetime
[改名]
[删除]
[移动]
";
$files[] = $line;
}
else if(preg_match("#\.(txt|inc|pl|cgi|asp|xml|xsl|aspx|cfm)#",$file)) //常用文本文件类型
{
$edurl = "view/file_manage_view.php?fmdo=edit&filename=".urlencode($file)."&activepath=".urlencode($activepath);
$line = "\n
$file
$filesize KB
$filetime
[编辑]
[改名]
[删除]
[移动]
";
$files[] = $line;
}
else if(preg_match("#\.(htm|html)#i",$file)) //网页类型
{
$edurl = "view/file_manage_view.php?fmdo=edit&filename=".urlencode($file)."&activepath=".urlencode($activepath);
$line = "\n
$file
$filesize KB
$filetime
[编辑]
[改名]
[删除]
[移动]
";
$files[] = $line;
}
else if(preg_match("#\.(php)#i",$file)) //php脚本类型
{
$edurl = "view/file_manage_view.php?fmdo=edit&filename=".urlencode($file)."&activepath=".urlencode($activepath);
$line = "\n
$file
$filesize KB
$filetime
[编辑]
[改名]
[删除]
[移动]
";
$files[] = $line;
}
else if(preg_match("#\.(js)#i",$file)) //js脚本类型
{
$edurl = "view/file_manage_view.php?fmdo=edit&filename=".urlencode($file)."&activepath=".urlencode($activepath);
$line = "\n
$file
$filesize KB
$filetime
[编辑]
[改名]
[删除]
[移动]
";
$files[] = $line;
}
else if(preg_match("#\.(css)#i",$file)) //css
{
$edurl = "view/file_manage_view.php?fmdo=edit&filename=".urlencode($file)."&activepath=".urlencode($activepath);
$line = "\n
$file
$filesize KB
$filetime
[编辑]
[改名]
[删除]
[移动]
";
$files[] = $line;
}
else
{
$line = "\n
$file
$filesize KB
$filetime
[改名]
[删除]
[移动]
";
$files[] = $line;
}
}
$dh->close();//关闭文件流
foreach($dirs as $dir)
{
echo $dir;
}
foreach($files as $file)
{
echo $file;
}
?>
[根目录]
[新建文件]
[新建目录]
[文件上传]
[空间检查]
这样基本就可以得到文件目录的视图了,接下来是文件的操作:
文件的操作和文件的操作视图分别写在file_manage_control.php和file_manage_view.php中
file_manage_control.php
read())
{
if($infile=="."||$infile=="..")
{
continue;
}
else if(is_file("$indir/$infile"))
{
unlink("$indir/$infile");
}
else
{
RmDirFiles("$indir/$infile");
}
}
$dh->close();
rmdir($indir);
return 1;
}
/*---------------
修改文件名
rename($oldname,$filename)
is_writable($filename)
----------------*/
if($fmdo=="rename")
{
$oldfile=$activepath.'/'.$_POST['oldfilename'];
$newfile=empty($_POST['newfilename']) ? $oldfile : $activepath.'/'.$_POST['newfilename'];
if(($newfile!=$oldfile) && is_writable($oldfile))
{
if(rename($oldfile,$newfile))
ShowMsg("文件名修改成功","../index.php?activepath=".$activepath);
}
else
ShowMsg("请检查文件名是否为空,或者文件是否有写入权限!","../index.php?activepath=".$activepath);
}
/*---------------
编辑文件
stripslashes($str)反引用一个引用字符串(一般也就是用来去掉字符串中\)
----------------*/
else if($fmdo=="edit")
{
$str=$_POST['str'];//获取编辑框的内容
$filename=str_replace("..","",$filename);//替换文件中的".."字符串
$file=$activepath.'/'.$filename;//文件的绝对地址
$str=stripslashes($str);
$fp=fopen($file,"w");//以写的方式打开文件,并返回文件句柄
if(fwrite($fp,$str))//写入文件(覆盖掉原有的文本内容)
{
ShowMsg("修改成功","../index.php?activepath=".$activepath);
}
else
{
ShowMsg("禁止修改!","../index.php?activepath=".$activepath);
}
fclose($fp);//关闭文件句柄
}
else if($fmdo=="del")
{
$file=$activepath.'/'.$filename;
if(is_file($file))
{
if(unlink($file))
{
ShowMsg("删除成功","../index.php?activepath=".$activepath);
}
else
{
ShowMsg("删除失败,请检查系统权限!","../index.php?activepath=".$activepath);
}
$t="文件";
}
else
{
$t="目录";
if(RmDirFiles($file))
{
ShowMsg("目录删除成功","../index.php?activepath=".$activepath);
}
else
{
ShowMsg("目录删除失败","../index.php?activepath=".$activepath);
}
}
}
else if($fmdo=="move")
{
$newpath=$_POST['newpath'];
if($newpath!="" && !preg_match("#\.\.#", $newpath))
{
$oldfile=$activepath."/$filename";
$newpath=str_replace("\\", "/", $newpath);
$newpath=preg_replace("#\/{1,}#" , "/" , $newpath);
$newpath=$activepath."/$newpath";
if(is_readable($oldfile) && is_readable($newpath) && is_writable($newpath))
{
if(is_dir($newpath))
{
$flag=copy($oldfile,$newpath."/$filename") ? 1 : 0;
}
else
{
mkdir($newpath);//mode在Windows下被忽略
$flag=copy($oldfile , $newpath."/$filename") ? 1 : 0;
}
unlink($oldfile);
}
}
if($flag==1)
ShowMsg("文件移动成功","../index.php?activepath=".$activepath);
else
ShowMsg("文件移动失败","../index.php?activepath=".$activepath);
}
else if($fmdo=="newdir")
{
$newpath=$_POST['newpath'];
if(!empty($newpath))
{
$newdirpath=$activepath."/$newpath";
if(mkdir($newdirpath))
{
ShowMsg("目录创建成功","../index.php?activepath=".$activepath);
}
else
ShowMsg("目录创建失败","../index.php?activepath=".$activepath);
}
}
else if($fmdo=="upload")
{
for($i=1;$i<50;$i++)
{
$upfile="upfile".$i;
if($_FILES[$upfile]['error']>0)
{
continue;
}
else
{
move_uploaded_file($_FILES[$upfile]['tmp_name'], $activepath.'/'.$_FILES[$upfile]['name']);
}
}
ShowMsg("文件上传成功","../index.php?activepath=".$activepath);
}
else if($fmdo=="space")
{
if($activepath=="")
{
$ecpath = "所有目录";
}
else
{
$ecpath = $activepath;
}
$titleinfo = "目录 $ecpath 空间使用状况:
";
$wintitle = "文件管理";
$wecome_info = "文件管理::空间大小检查 [文件浏览器]";
$activepath=$cfg_basedir.$activepath;
$space = new SpaceUse;
$space->checksize($activepath);
$total=$space->totalsize;
$totalkb=$space->setkb($total);
$totalmb=$space->setmb($total);
$win = new OxWindow();
$win->Init("","js/blank.js","POST");
$win->AddTitle($titleinfo);
$win->AddMsgItem(" $totalmb M
$totalkb KB
$total 字节");
$winform = $win->GetWindow("");
$win->Display();
}
?>
文件操作视图模板类oxwindow.class.php
myWin .= "\r\n";
$this->formName = $formname;
$this->myWin .= "\r\n";
}
}
/**
* 增加自定义JS脚本
*
* @param string $scripts
* @return void
*/
function SetCheckScript($scripts)
{
// strpos()返回一个字符串在另一个字符串中第一次出现的位置
$pos = strpos($this->myWin,$this->tmpCode);
if($pos > 0)//myWin中确实有tmpCode
{
//substr_replace(要检查的字符串,要替换的字符串,替换的位置,替换的长度)
$this->myWin = substr_replace($this->myWin,$scripts,$pos,strlen($this->tmpCode));
}
}
/**
* 获取窗口
*
* @param string $wintype 菜单类型
* @param string $msg 短消息
* @param bool $isform 是否是表单
* @return string
*/
function GetWindow($wintype="save", $msg="", $isform=true)
{
// global $cfg_plus_dir;
$this->StartWin();
$this->myWin .= $this->myWinItem;
if($wintype!="")
{
if($wintype!="hand")
{
$this->myWin .= "
";
}
else
{
if($msg!='')
{
$this->myWin .= "$msg ";
}
else
{
$this->myWin .= '';
}
}
}
$this->CloseWin($isform);
return $this->myWin;
}
/**
* 显示页面
*
* @access public
* @param string $modfile 模型模板
* @return string
*/
function Display($modfile="")
{
global $welcome_info ,$wintitle;
if(empty($welcome_info))
{
$welcome_info = "通用对话框:";
}
if($modfile=='')
{
require_once("../view/plus/win_templet.htm");
}
else
{
require_once($modfile);
}
}
}
/**
* 显示一个不带表单的普通提示
*
* @access public
* @param string $msg 消息提示信息
* @param string $title 提示标题
* @return string
*/
function ShowMsgWin($msg, $title)
{
$win = new OxWindow();
$win->Init();
$win->mainTitle = "系统提示:";
$win->AddTitle($title);
$win->AddMsgItem("$msg");
$winform = $win->GetWindow("hand");
$win->Display();
}
?>
文件编辑模板:file_edit.htm
文件管理器
◇文件管理 >> 修改/新建文件
文件上传
文件上传
myWin; ?>
developed by sevend
文件浏览器]";
$win = new OxWindow();
$win->Init("file_manage_control.php","js/blank.js","POST");
$win->AddHidden("fmdo",$fmdo);
$win->AddHidden("activepath",$activepath);
$win->AddHidden("filename",$filename);
$win->AddTitle("更改文件名,当前路径:$ndirstring");
$win->AddItem("旧名称:","");
$win->AddItem("新名称:","");
$winform = $win->GetWindow("ok");
$win->Display();
// echo $win->myWin;
}
//修改文件内容
else if($fmdo=="edit")
{
if(!isset($backurl))
{
$backurl="";
}
if(!isset($ishead))
{
$ishead="";
}
$activepath=str_replace("..", "", $activepath);
$filename=str_replace("..","",$filename);
$file=$activepath.'/'.$filename;
$content="";
if(is_file($file))
{
$fp=fopen($file,"r");
$content=fread($fp,filesize($file));
fclose($fp);
$content=htmlspecialchars($content);//html格式转换
}
$contentView="\r\n";
require_once("plus/file_edit.htm");
}
//删除文件
else if($fmdo=="del")
{
$wintitle = " 文件管理";
$wecome_info = " 文件管理::删除文件 [文件浏览器]";
$win = new OxWindow();
$win->Init("file_manage_control.php","js/blank.js","POST");
$win->AddHidden("fmdo",$fmdo);
$win->AddHidden("activepath",$activepath);
$win->AddHidden("filename",$filename);
if(@is_dir($activepath."/$filename"))
{
$wmsg = "你确信要删除目录:$filename 吗?";
}
else
{
$wmsg = "你确信要删除文件:$filename 吗?";
}
$win->AddTitle("删除文件确认");
$win->AddMsgItem($wmsg,"50");
$winform = $win->GetWindow("ok");
$win->Display();
}
else if($fmdo=="move")
{
$wintitle = " 文件管理";
$wecome_info = " 文件管理::移动文件 [文件浏览器]";
$win = new OxWindow();
$win->Init("file_manage_control.php","js/blank.js","POST");
$win->AddHidden("fmdo",$fmdo);
$win->AddHidden("activepath",$activepath);
$win->AddHidden("filename",$filename);
$win->AddTitle("新位置前面不加'/'表示相对于当前位置,加'/'表示相对于根目录。");
$win->AddItem("被移动文件:",$filename);
$win->AddItem("当前位置:",$activepath);
$win->AddItem("新位置:","");
$winform = $win->GetWindow("ok");
$win->Display();
}
else if($fmdo=="newdir")
{
if($activepath=="") $activepathname="根目录";
else $activepathname=$activepath;
$wintitle = " 文件管理";
$wecome_info = " 文件管理::新建目录 [文件浏览器]";
$win = new OxWindow();
$win->Init("file_manage_control.php","js/blank.js","POST");
$win->AddHidden("fmdo",$fmdo);
$win->AddHidden("activepath",$activepath);
$win->AddTitle("当前目录 $activepathname ");
$win->AddItem("新目录:","");
$winform = $win->GetWindow("ok");
$win->Display();
}
else if($fmdo=="newfile")
{
$filename="file.txt";
$contentView = "\r\n";
require_once("plus/file_edit.htm");
}
else if($fmdo=="upload")
{
require_once("plus/file_upload.htm");
}
else if($fmdo=="space")
{
if($activepath=="")
{
$ecpath = "所有目录";
}
else
{
$ecpath = $activepath;
}
$titleinfo = "目录 $ecpath 空间使用状况:
";
$wintitle = "文件管理";
$wecome_info = "文件管理::空间大小检查 [文件浏览器]";
// $activepath=$cfg_basedir.$activepath;
$size=0;
$size=getDirSize($activepath);
if($size>pow(2,10))
{
$sizekb=round($size/pow(1024,1),2);
}
$sizemb=round($sizekb/1024,2);
$str=$size." 字节
".$sizekb." KB
".$sizemb." MB
";
$win = new OxWindow();
$win->Init("","js/blank.js","POST");
$win->AddTitle($titleinfo);
$win->AddMsgItem($str);
$winform = $win->GetWindow("");
$win->Display();
}
function getDirSize($dirpath)
{
global $size;
if(is_file($dirpath))
{
$size+=filesize($dirpath);
}
else
{
$dh=dir($dirpath);
while($infile=$dh->read())
{
if($infile=="."||$infile=="..")
{
continue;
}
else if(is_file("$dirpath/$infile"))
{
$size+=filesize("$dirpath/$infile");
}
else
{
getDirSize("$dirpath/$infile");
}
}
$dh->close();
}
return $size;
}
?>
show_msg.php
\r\n\r\n温馨提示 \r\n\r\n";
$htmlhead .= " \r\n\r\n\r\n\r\n\r\n \r\n\r\n