<?php
//在线文件编辑器
/*-------------------------------------
使用工厂设计模式,MVC实现
*/
class onlineEditor{
//设置全局变量路径
public $filePath = null;
//设置过滤信息
private $fileFilter = array(
'onlineEditor.php',
'viewEditor.html',
'index.php',
'.',
'..'
);
//构造函数必须是私有的在单例设计模式中
function __construct($filePath){
$this->filePath = $filePath;
}
//当本类销毁的时候进行的操作
function __destruct(){
// echo $this->filePath;
}
//获取文件的内容
function getContent($filePath){
if (!isset($filePath)) {
} else{
//获取文件内容
$fileContent = file_get_contents($filePath);
return $fileContent;
}
}
//放入文件内容
function putContent($filePath,$fileContent){
file_put_contents($filePath, $fileContent);
}
//判断目录是否存在
private function judgeExist(){
//判断目录是否为空或者没有文件
if(is_dir($this->filePath) && file_exists($this->filePath)){
return true;
} else{
return false;
}
}
//创建文件
function createFile($filename){
if(!file_exists($filename)){
fopen($filename, "w+");
}
else{
echo "<a href = 'index.php' >点此返回</a>";
die("文件已经存在");
}
}
//删除文件
function delFile($filename){
if(file_exists($filename)){
unlink($filename);
}
}
//主函数
function main(){
if($this->judgeExist()){
//获取打开文件夹对象
$fileOpen = opendir($this->filePath);
$fileMes = array();
$i = 0;
//遍历文件夹
while ($file = readdir($fileOpen)) {
//过滤
if(in_array($file, $this->fileFilter)){
continue;
}
$fileMesPush = array(
'fileCode' => $i,
'fileName' => $file,
'fileType' => fileType($file),
'fileSize' => fileSize($file),
'filemtime' => filemtime($file)
);
array_push($fileMes, $fileMesPush);
$i++;
}
//关闭文件
return $fileMes;
fclose($fileOpen);
} else{
die("不存在此文件夹");
}
}
}
?>
index.php<?php
$dirPath = "./"; //设置操作目录 可改成自选操作目录
$action = null;
//引入文件
require "./onlineEditor.php";
//获得onlineEditor对象
$onlineEditor = new onlineEditor($dirPath);
$fileMes = $onlineEditor->main();
//处理文件路径
function subFilePath($dirPath,$filename){
// echo $dirPath . $filename;
return $dirPath . $filename;
}
//初始化
if(array_key_exists('action', $_GET)){
switch ($_GET['action']) {
case 'updata':
$action = 'updata';
break;
case 'del':
$onlineEditor->delFile(subFilePath($dirPath,$_GET['filename']));
$action = 'del';
echo subFilePath($dirPath,$_GET['filename']);
echo "<script>location.href = 'index.php';</script>";
break;
}
} else{
$action = null;
}
if(array_key_exists('action', $_POST)){
switch ($_POST['action']) {
case 'create':
$onlineEditor->createFile(subFilePath($dirPath,$_POST['filename']));
echo "<script>location.href = 'index.php';</script>";
break;
}
}
//获取文件内容
if(array_key_exists('filename', $_GET) && $_GET['action'] == 'updata'){
$root = subFilePath($dirPath,$_GET['filename']);
$fileContent = $onlineEditor -> getContent($root);
} else
$fileContent = "坚持就是胜利";
if (array_key_exists('filecontent', $_POST)) {
// var_dump($_POST);
$onlineEditor->putContent(subFilePath($dirPath,$_POST['filename']),$_POST['filecontent']);
echo "<script>location.href = 'index.php';</script>";
}
//引入界面
require "./viewEditor.html";
// print_r($fileMes);
?>
viewEditor.php<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>在线文件编辑器</title>
<style>
*{margin: 0;padding: 0;}
table{text-align: center;}
.fileMes{width: 800px;height: auto;margin: 0 auto;background: #abcdef;}
.fileMes table tr{height: 30px;}
.fileMes table tr td{border: 1px #fff solid;padding: 10px;}
.updata{width: 800px;height:auto;margin: 0 auto;background: #fff;}
.updata textarea{width: 100%;height: 300px;text-align: left;}
.btn{width:100px;height: 30px; }
.createFile{width:500px;height:auto;margin: 0 auto;margin-bottom:20px;margin-left:400px; }
</style>
<script type="text/javascript">
function backIndex(){
location.href = 'index.php';
}
function clearTxt(){
document.getElementById('txt');
txt.innerHTML = "";
}
</script>
</head>
<body bgcolor="#CCCCCC">
<div style="width:1000px;height:auto;background:#CCCCCC;margin:0 auto;">
<br/><br/>
<h2 align="center" style="font-size:26px;font-weight:bold;">文件在线管理</h2>
<br/><br/>
<hr/>
<br/><br/>
<?php
if($action == 'updata'){
?>
<div class="updata">
<form action="index.php" method="post" name="myform">
<textarea id="txt" name="filecontent"><?php echo $fileContent; ?></textarea>
<input type="hidden" value="<?php echo $_GET['filename']; ?>" name="filename" />
<input class="btn" type="button" onclick="backIndex();" value="返回" name="back" />
<input class="btn" style="float:right;margin-left:20px;" onclick="clearTxt()" type="button" value="清空" />
<a href="index.php"><input class="btn" style="float:right;" type="submit" value="保存" /></a>
</form>
</div>
<br/><br/>
<?php
}
?>
<!--创建文件-->
<div class="createFile">
<form action="index.php" method="post" name="myform2">
<input type="hidden" name="action" value="create" />
<input style="width:200px;height:30px;" type="text" name="filename" />
<input class="btn" type="submit" value="创建" />
</form>
</div>
<div class="fileMes">
<table style="width:100%;height:auto;">
<tr style="background:#0099CC;">
<th>序号</th><th>文件名</th><th>文件类型</th><th>文件大小</th><th>创建日期</th><th colspan="2">其他操作</th>
</tr>
<?php
foreach ($fileMes as $v){
?>
<tr>
<td><?php echo $v['fileCode'];?></td>
<td><?php echo $v['fileName'];?></td>
<td><?php echo $v['fileType'];?></td>
<td><?php echo $v['fileSize'];?></td>
<td><?php echo date("Y-m-d",$v['filemtime']);?></td>
<td><a href=index.php?action=updata&filename=<?php echo $v['fileName']; ?> >修改</a></td>
<td><a href=index.php?action=del&filename=<?php echo $v['fileName']; ?> >删除</a></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</body>
</html>
内容杂,详情请下载代码。简单的文件在线管理.zip ( 4.36 KB 下载:123 次 )