网站在线文件管理

jerry thinkphp 2015年11月19日 收藏
简单在线文件系统管理
文件的遍历,我还没写完找时间写
onlineEditor.php文件代码
  1. <?php

  2. //在线文件编辑器
  3. /*-------------------------------------
  4.    使用工厂设计模式,MVC实现
  5. */

  6. class onlineEditor{

  7.     //设置全局变量路径
  8.     public $filePath = null;
  9.     //设置过滤信息
  10.     private $fileFilter = array(
  11.         'onlineEditor.php',         
  12.         'viewEditor.html',
  13.         'index.php',
  14.         '.',
  15.         '..'
  16.     );
  17.     
  18.     //构造函数必须是私有的在单例设计模式中
  19.     function __construct($filePath){
  20.         $this->filePath = $filePath;
  21.     }

  22.     //当本类销毁的时候进行的操作
  23.     function __destruct(){
  24.         // echo $this->filePath;
  25.     }

  26.     //获取文件的内容
  27.     function getContent($filePath){
  28.         if (!isset($filePath)) {
  29.             
  30.         } else{
  31.             //获取文件内容
  32.             $fileContent = file_get_contents($filePath);
  33.             return $fileContent;
  34.         }
  35.     }

  36.     //放入文件内容
  37.     function putContent($filePath,$fileContent){
  38.         file_put_contents($filePath, $fileContent);
  39.     }

  40.     //判断目录是否存在
  41.     private function judgeExist(){
  42.         //判断目录是否为空或者没有文件
  43.         if(is_dir($this->filePath) && file_exists($this->filePath)){
  44.             return true;
  45.         } else{
  46.             return false;
  47.         }
  48.     }

  49.     //创建文件
  50.     function createFile($filename){
  51.         if(!file_exists($filename)){
  52.             fopen($filename, "w+");
  53.         }
  54.             
  55.         else{
  56.             echo "<a href = 'index.php' >点此返回</a>";
  57.              die("文件已经存在");
  58.         }
  59.             
  60.     }
  61.     //删除文件
  62.     function delFile($filename){
  63.         if(file_exists($filename)){
  64.             unlink($filename);
  65.         }
  66.     }

  67.     //主函数
  68.     function main(){
  69.         if($this->judgeExist()){
  70.             //获取打开文件夹对象
  71.             $fileOpen = opendir($this->filePath);
  72.             $fileMes = array();
  73.             $i = 0;
  74.             //遍历文件夹
  75.             while ($file = readdir($fileOpen)) {
  76.                 
  77.                 //过滤
  78.                 if(in_array($file, $this->fileFilter)){
  79.                     continue;
  80.                 }
  81.                 
  82.                 $fileMesPush = array(
  83.                     'fileCode'  => $i,
  84.                     'fileName'  => $file,
  85.                     'fileType'  => fileType($file),
  86.                     'fileSize'  => fileSize($file),
  87.                     'filemtime' => filemtime($file)
  88.                 );
  89.                 
  90.                 array_push($fileMes, $fileMesPush);
  91.                 $i++;
  92.             }
  93.             //关闭文件
  94.            
  95.             return $fileMes;
  96.             fclose($fileOpen);
  97.         } else{
  98.             die("不存在此文件夹");
  99.         }
  100.     }

  101. }

  102. ?>
index.php
  1. <?php
  2.     

  3.     $dirPath = "./";  //设置操作目录 可改成自选操作目录
  4.     $action = null;

  5.     //引入文件
  6.     require "./onlineEditor.php";

  7.     //获得onlineEditor对象
  8.     $onlineEditor = new onlineEditor($dirPath);
  9.     $fileMes = $onlineEditor->main();

  10.     //处理文件路径
  11.     function subFilePath($dirPath,$filename){
  12.         // echo $dirPath . $filename;
  13.         return $dirPath . $filename;
  14.     }

  15.     //初始化
  16.     if(array_key_exists('action', $_GET)){
  17.         switch ($_GET['action']) {
  18.             case 'updata':
  19.                 $action = 'updata';
  20.                 break;
  21.             case 'del':
  22.                 $onlineEditor->delFile(subFilePath($dirPath,$_GET['filename']));
  23.                 $action = 'del';
  24.                 echo subFilePath($dirPath,$_GET['filename']);
  25.                 echo "<script>location.href = 'index.php';</script>";
  26.                 break;
  27.         }
  28.     } else{
  29.         $action = null;
  30.     }

  31.     if(array_key_exists('action', $_POST)){
  32.         switch ($_POST['action']) {
  33.             case 'create':
  34.                 $onlineEditor->createFile(subFilePath($dirPath,$_POST['filename']));
  35.                 echo "<script>location.href = 'index.php';</script>";
  36.                 break;
  37.         }
  38.     }

  39.     //获取文件内容
  40.     if(array_key_exists('filename', $_GET) && $_GET['action'] == 'updata'){
  41.         $root = subFilePath($dirPath,$_GET['filename']);
  42.         $fileContent = $onlineEditor -> getContent($root);
  43.     } else
  44.         $fileContent = "坚持就是胜利";

  45.     if (array_key_exists('filecontent', $_POST)) {
  46.         // var_dump($_POST);
  47.         $onlineEditor->putContent(subFilePath($dirPath,$_POST['filename']),$_POST['filecontent']);
  48.         echo "<script>location.href = 'index.php';</script>";
  49.     }    


  50.     //引入界面
  51.     require "./viewEditor.html";

  52.     // print_r($fileMes);



  53. ?>
viewEditor.php
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  4.     <title>在线文件编辑器</title>
  5.     <style>
  6.         *{margin: 0;padding: 0;}
  7.         table{text-align: center;}
  8.         .fileMes{width: 800px;height: auto;margin: 0 auto;background: #abcdef;}
  9.         .fileMes table tr{height: 30px;}
  10.         .fileMes table tr td{border: 1px #fff solid;padding: 10px;}
  11.         .updata{width: 800px;height:auto;margin: 0 auto;background: #fff;}
  12.         .updata textarea{width: 100%;height: 300px;text-align: left;}
  13.         .btn{width:100px;height: 30px; }
  14.         .createFile{width:500px;height:auto;margin: 0 auto;margin-bottom:20px;margin-left:400px; }
  15.     </style>
  16.     <script type="text/javascript">
  17.     function backIndex(){
  18.         location.href = 'index.php';
  19.     }
  20.     function clearTxt(){
  21.         document.getElementById('txt');
  22.         txt.innerHTML = "";
  23.     }
  24.     </script>

  25. </head>

  26. <body bgcolor="#CCCCCC">
  27.     <div style="width:1000px;height:auto;background:#CCCCCC;margin:0 auto;">
  28.     <br/><br/>
  29.     <h2 align="center" style="font-size:26px;font-weight:bold;">文件在线管理</h2>
  30.     <br/><br/>
  31.     <hr/>
  32.     <br/><br/>
  33.     <?php
  34.     if($action == 'updata'){
  35.     ?>
  36.         <div class="updata">
  37.             <form action="index.php" method="post" name="myform">
  38.                 <textarea id="txt" name="filecontent"><?php echo $fileContent; ?></textarea>
  39.                 <input type="hidden" value="<?php echo $_GET['filename']; ?>" name="filename" />
  40.                 <input class="btn" type="button" onclick="backIndex();" value="返回" name="back" />
  41.                 <input class="btn" style="float:right;margin-left:20px;" onclick="clearTxt()" type="button" value="清空" />
  42.                 <a href="index.php"><input class="btn" style="float:right;" type="submit" value="保存" /></a>
  43.             </form>
  44.         </div>
  45.         <br/><br/>
  46.     <?php
  47.     }
  48.     ?>
  49.     <!--创建文件-->
  50.     <div class="createFile">
  51.         <form action="index.php" method="post" name="myform2">
  52.             <input  type="hidden" name="action" value="create" />
  53.             <input style="width:200px;height:30px;" type="text" name="filename" />
  54.             <input class="btn" type="submit" value="创建" />
  55.         </form>
  56.     </div>
  57.     <div class="fileMes">
  58.         <table style="width:100%;height:auto;">
  59.             <tr style="background:#0099CC;">
  60.                 <th>序号</th><th>文件名</th><th>文件类型</th><th>文件大小</th><th>创建日期</th><th colspan="2">其他操作</th>
  61.             </tr>
  62.             <?php
  63.                 foreach ($fileMes as $v){ 
  64.             ?>
  65.                 <tr>
  66.                     <td><?php echo $v['fileCode'];?></td>
  67.                     <td><?php echo $v['fileName'];?></td>
  68.                     <td><?php echo $v['fileType'];?></td>
  69.                     <td><?php echo $v['fileSize'];?></td>
  70.                     <td><?php echo date("Y-m-d",$v['filemtime']);?></td>
  71.                     <td><a href=index.php?action=updata&filename=<?php echo $v['fileName']; ?> >修改</a></td>
  72.                     <td><a href=index.php?action=del&filename=<?php echo $v['fileName']; ?> >删除</a></td>
  73.                 </tr>
  74.             <?php
  75.                 }
  76.             ?>
  77.         </table>
  78.     </div>
  79.     </div>
  80. </body>

  81. </html>
内容杂,详情请下载代码。

附件简单的文件在线管理.zip ( 4.36 KB 下载:123 次 )