首页 > php教程 > php手册 > 正文

网站在线文件管理

php中文网
发布: 2016-06-07 11:39:53
原创
2049人浏览过

简单在线文件系统管理
文件的遍历,我还没写完找时间写
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-&gt;filePath = $filePath;<br>     }<br><br>     //当本类销毁的时候进行的操作<br>     function __destruct(){<br>         // echo $this-&gt;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-&gt;filePath) &amp;&amp; file_exists($this-&gt;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-&gt;judgeExist()){<br>             //获取打开文件夹对象<br>             $fileOpen = opendir($this-&gt;filePath);<br>             $fileMes = array();<br>             $i = 0;<br>             //遍历文件夹<br>             while ($file = readdir($fileOpen)) {<br>                 <br>                 //过滤<br>                 if(in_array($file, $this-&gt;fileFilter)){<br>                     continue;<br>                 }<br>                 <br>                 $fileMesPush = array(<br>                     'fileCode'  =&gt; $i,<br>                     'fileName'  =&gt; $file,<br>                     'fileType'  =&gt; fileType($file),<br>                     'fileSize'  =&gt; fileSize($file),<br>                     'filemtime' =&gt; 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> ?&gt;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-&gt;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-&gt;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-&gt;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) &amp;&amp; $_GET['action'] == 'updata'){<br>         $root = subFilePath($dirPath,$_GET['filename']);<br>         $fileContent = $onlineEditor -&gt; getContent($root);<br>     } else<br>         $fileContent = "坚持就是胜利";<br><br>     if (array_key_exists('filecontent', $_POST)) {<br>         // var_dump($_POST);<br>         $onlineEditor-&gt;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> ?&gt;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>     ?&gt;<br>         <div> <br>             <form> <br>                 <textarea><?php echo $fileContent; ?></textarea><br>                 <input>" name="filename" /&gt;<br>                 <input><br>                 <input><br>                 <a><input></a><br>             </form> <br>         </div> <br>         <br><br><br>     <?php <br />     }<br>     ?&gt;<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>             ?&gt;<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> &gt;修改</a></td> <br>                     <td><a> &gt;删除</a></td> <br>                 </tr> <br>             <?php <br />                 }<br>             ?&gt;<br>         </table> <br>     </div> <br>     </div> <br><br><br>内容杂,详情请下载代码。

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

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

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号