Home  >  Article  >  php教程  >  网站在线文件管理

网站在线文件管理

WBOY
WBOYOriginal
2016-06-07 11:39:531708browse

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

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

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn