• Web 在线文件管理器学习笔记与总结(4)查看文件内容


    ② 查看文件内容

    a.通过 file_get_contents($filename) 得到文件内容

    b.通过 highlight_string($string) 或者 highlight_file($filename) 显示内容。使用 PHP 内置的语法高亮器所定义的颜色,打印输出或者返回输出或者返回语法高亮版本的 PHP 代码

    当使用 file_get_contents 读取(php)文件的内容并且显示在页面上,可以使用 <textarea></textarea> 包裹输出的内容:

    $content = file_get_contents($filename);
    echo "<textarea cols='100' rows='10'>{$content}</textarea>";

    index.php:

      1 <?php 
      2 require 'dir.func.php';
      3 require 'file.func.php';
      4 require 'common.func.php';
      5 $path = 'file';
      6 $info = readDirectory($path);
      7 
      8 $act = @$_REQUEST['act'];
      9 $filename = @$_REQUEST['filename'];
     10 //跳转变量
     11 $redirect = "index.php?path={$path}";
     12 if($act == 'createFile'){
     13     //创建文件
     14     $mes = createFile($path.'/'.$filename);
     15     alertMes($mes,$redirect);
     16 }else if($act == 'showContent'){
     17 //查看文件内容
     18     $content=file_get_contents($filename);
     19     //echo "<textarea readonly='readonly' cols='100' rows='10'>{$content}</textarea>";
     20     //高亮显示PHP代码
     21     //高亮显示字符串中的PHP代码
     22     if(strlen($content)){
     23     $newContent=highlight_string($content,true);
     24     //高亮显示文件中的PHP代码
     25     //highlight_file($filename);
     26     $str=<<<EOF
     27     <table width='100%' bgcolor='pink' cellpadding='5' cellspacing="0" >
     28         <tr>
     29             <td>$newContent</td>
     30         </tr>
     31     </table>
     32 EOF;
     33         echo $str;
     34     }else{
     35         alertMes("文件没有内容,请编辑再查看!",$redirect);
     36     }
     37 }
     38 ?>
     39 <!DOCTYPE html>
     40 <html>
     41 <head>
     42 <meta charset="UTF-8">
     43 <title>Insert title here</title>
     44 <link rel="stylesheet" href="cikonss.css" />
     45 <link rel="stylesheet" href="common.css" />
     46 </head>
     47 <body>
     48 <h1>在线文件管理器</h1>
     49 <div id="top">
     50     <ul id="navi">
     51         <li><a href="index.php" title="主目录"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-home"></span></span></a></li>
     52         <li><a href="#" onclick="show('createFile')" title="新建文件" ><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-file"></span></span></a></li>
     53         <li><a href="#" title="新建文件夹"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-folder"></span></span></a></li>
     54         <li><a href="#" title="上传文件"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-upload"></span></span></a></li>
     55         <li><a href="#" title="返回上级目录"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span class="icon-arrowLeft"></span></span></a></li>
     56     </ul>
     57 </div>
     58 <form action="index.php" method="post" enctype="multipart/form-data">
     59 <table width='100%' border='1' cellpadding="5" cellspacing="0" bgcolor="#abcdef" align="center">
     60     <tr id="createFolder"  style="display:none;">
     61         <td>请输入文件夹名称</td>
     62         <td >
     63             <input type="text" name="dirname" />
     64             <input type="hidden" name="path"  value="<?php echo $path;?>"/>
     65             <input type="submit"  name="act"  value="创建文件夹"/>
     66         </td>
     67     </tr>
     68     <tr id="createFile"  style="display:none;">
     69         <td>请输入文件名称</td>
     70         <td >
     71             <input type="text"  name="filename" />
     72             <input type="hidden" name="path" value="<?php echo $path;?>"/>
     73             <input type="hidden" name='act' value='createFile'/>
     74             <input type="submit" value="创建文件" />    
     75         </td>
     76     </tr>
     77     <tr id="uploadFile" style="display:none;">
     78         <td >请选择要上传的文件</td>
     79         <td ><input type="file" name="myFile" />
     80             <input type="submit" name="act" value="上传文件" />    
     81         </td>
     82     </tr>
     83     <tr align="center">
     84         <td>编号</td>
     85         <td>名称</td>
     86         <td>类型</td>
     87         <td>大小</td>
     88         <td>可读</td>
     89         <td>可写</td>
     90         <td>可执行</td>
     91         <td>创建时间</td>
     92         <td>修改时间</td>
     93         <td>访问时间</td>
     94         <td>操作</td>
     95     </tr>
     96     <?php 
     97         if($info['file']){
     98             $i = 1;
     99             foreach($info['file'] as $val){
    100                 $p = $path.'/'.$val;
    101     ?>
    102     <tr align="center">
    103         <td><?php echo $i;?></td>
    104         <td><?php echo $val;?></td>
    105         <td><?php $src = filetype($p) == 'file'?'file_ico.png':'folder_ico.png';?><img src="images/<?php echo $src;?>" alt="" title='文件'></td>
    106         <td><?php echo transByte(filesize($p));?></td>
    107         <td><?php $src = is_readable($p)?'correct.png':'error.png';?><img src="images/<?php echo $src;?>" width="32" alt="" title='可读'></td>
    108         <td><?php $src = is_writeable($p)?'correct.png':'error.png';?><img src="images/<?php echo $src;?>" width="32" alt="" title='可写'></td>
    109         <td><?php $src = is_executable($p)?'correct.png':'error.png';?><img src="images/<?php echo $src;?>" width="32" alt="" title='可写'></td>
    110         <td><?php echo date('Y-m-d H:i:s',filectime($p));?></td>
    111         <td><?php echo date('Y-m-d H:i:s',filemtime($p));?></td>
    112         <td><?php echo date('Y-m-d H:i:s',fileatime($p));?></td>
    113         <td>
    114             <a href="index.php?act=showContent&filename=<?php echo $p;?>" title='查看'><img src="images/show.png" width="32" alt=""></a>
    115             <a href="" title='修改'><img src="images/edit.png" width="32" alt=""></a>
    116             <a href="" title='重命名'><img src="images/rename.png" width="32" alt=""></a>
    117             <a href="" title='复制'><img src="images/copy.png" width="32" alt=""></a>
    118             <a href="" title='剪切'><img src="images/cut.png" width="32" alt=""></a>
    119             <a href="" title='删除'><img src="images/delete.png" width="32" alt=""></a>
    120             <a href="" title='下载'><img src="images/download.png" width="32" alt=""></a>
    121         </td>
    122     </tr>
    123     <?php            
    124                 $i++;
    125             }
    126         }
    127     ?>
    128 </table>
    129 </form>
    130 <script src='common.js'></script>
    131 </body>
    132 </html>
    View Code
  • 相关阅读:
    What's New in Chrome DevTools All In One
    User Timing API All In One
    Tailwind CSS All In One
    CSS 3D rotate with mouse move All In One
    Event Timing API All In One
    Long Tasks API All In One
    js get window custom global variables All In One
    Web 前端广告落地页性能监控系统 All In One
    Element Timing API All In One
    base64编码
  • 原文地址:https://www.cnblogs.com/dee0912/p/4385493.html
Copyright © 2020-2023  润新知