Hot For Coding

PHP文件管理系统代码共享

这是最早用PHP写过的一个文件管理系统,管理的是Web服务器端的文件

功能主要有

  • 创建文件
  • 删除文件
  • 编辑文件
  • 地址栏
  • 文件列表
  • 文件夹进入
  • 文件夹创建
  • 文件夹删除
  • 图片显示

此系统仅运行在WIN平台上

PHP code

<?php
/*
 * 简单文件管理操作网页版
 * 琼台博客 www.qttc.net
 * 
 * 功能:
 *    文件删除
 *    文件添加
 *    文本编辑
 *    文件夹添加
 *    文件夹删除
 *    可以在地址栏输入路径访问
 *    项目总数提示
 *    文件总数提示
 *    文件夹总数提示
 *    剩余磁盘空间提示
 *    磁盘总空间提示
 *    当前目录大小显示
 *
 * 日期:2011-05-20
 */
 
// 参考视频 编写一个简单的文件管理系统
 
define("BANQUAN","__ V2.3 | MSN:mail@lizhong.me | Power by qttc.net");

if(!(isset($tijiao)) && !(isset($dirr))){
  $dirr="d:/";
}

// 判断提交值进行调取函数动作*******************************
 
if($tijiao){
 
  if($_GET["add"]=="Please improt filename"){ // 判断添加文件
    $error="Warning:Please in the improt right's filename";
    $dirr=str_replace("\\","/",$_GET["url"]);
  }elseif($_GET["add"]){
    $error=action($subb);
    $dirr=str_replace("\\","/",$_GET["url"]);
  }elseif($_GET["del"]){        // 判断删除文件
    $yest=action($subb);
    $dirr=$_GET["url"];
  }elseif($_GET["adddir"]){  // 判断添加文件夹
    $dirr=$_GET["url"];
    if($_GET["adddir"]=="Please improt directroyname"){
      $error="Warning: Please in the improt right's Directroyname";
      $dirr=str_replace("\\","/",$_GET["url"]);
    }else{
      $error=action($subb);    
    }
  }elseif($_GET["info"]){     // 进入文件夹
    $dirr=str_replace("//","/",$_GET["info"]);
  }elseif($_GET["dirr"]){ // 判断路径
    if(file_exists($_GET["dirr"]) && is_dir($_GET["dirr"])){
      $dirr=str_replace("\\","",$_GET["dirr"]);
    }else{
      $dirr=str_replace("\\","",$_GET["dirr"]);
      $adderres="Sorry: No find the directroy of \"".$_GET["dirr"]."\"";
    }
  }elseif($_GET["edit"]){        // 判断编辑文本
    $dirr=str_replace("\\","/",dirname($_GET["url"]));
    $edit=$_GET["edit"];
    $urllee=$_GET["url"];
    $editfile=$urllee."/".$edit;
  }elseif($_POST["editsave"]){    // 写入文本
    $dirr=str_replace("\\","/",dirname($_POST["editsave"]));
    $newconten=$_POST["newconten"];
    $wenjianmingming=basename($_POST["editsave"]);
    $xierulala=fopen($_POST["editsave"],"r+");
    fwrite($xierulala,$newconten);
    fclose($xierulala);
    $yest="\"$wenjianmingming is saved!";
  }elseif($_GET["viw"]){         // 打开照片
    $dirr=str_replace("\\","/",$_GET["url"]);
    $viw=$_GET["viw"];
    $chatoububu=dirname($dirr);
    $phototo=$chatoububu."/".$viw;
  }elseif($_GET["tijiao"]=="photo"){
    $dirr=str_replace("\\","/",$_GET["url"]);    
  }
}

// 编辑文本处理函数**************************************

// 递归处理删除文件夹函数***********************************
 
function deldir($deldir){
 
  $dell=opendir($deldir); // 打开文件夹
  while($fdell=readdir($dell)){
    if($fdell!="." && $fdell!=".."){
      $deldirr=$deldir."/".$fdell;
      if(is_dir($deldirr)){
        deldir($deldirr);    
      }elseif(is_file($deldirr)){
        unlink($deldirr);    
      }    
    }
  }

  closedir($dell);
  rmdir($deldir);
}

// 递归统计文件夹大小函数***********************************
 
function dirsize($muludir){
  $b=opendir($muludir);
  while($a=readdir($b)){
    if($a!="." && $a!=".."){
      $aa=$muludir."/".$a;
      if(is_dir($aa)){
        $sum+=dirsize($aa);
      }elseif(is_file($aa)){
        $sum+=filesize($aa);
      }
    }
  }

  closedir($b);
  return $sum;
}

// 处理函数****************************************************
 
function action($sub){
  $url=$_get["url"];
  switch($sub){
    
    // 执行添加文件命令条
    case 1:
      $add=$_GET["add"];
      $dirr=$_GET["url"];
      $newfile=$dirr."/".$add;

      if(!file_exists($newfile)){
        fopen("$newfile","w");
        $dirr=dirname($newfile);
        $error="\"".$add."\" file set up already";
        return $error;
        break;
      }else{
        $error="Sorry : The \"$add\" is existed!";
        return $error;
        break;
      }
    
    // 执行删除文件命令条
    case 2:
      $url=$_GET["url"];
      $del=$_GET["del"];
      $delfile=$url."/".$del;
      if(file_exists($delfile)){
        unlink($delfile);
        $yest="The \"$del\" File deleted";
        return $yest;
        break;
      }else{
        $yest="Sorry : The File \"$del\" no exist!";
        return $yest;
        break;
      }

    // 执行添加文件夹命令条
    case 3:
      $dirr=$_GET["url"];
      $adddir=$_GET["adddir"];
      $adddor=$dirr."/".$adddir;
      if(!file_exists($adddor)){
        mkdir($adddor);
        $dirr=dirname($adddor);
        $error="\"".$adddir."\" Directroy set up already";
        return $error;
        break;
      }else{
        $error="Sorry : The directroy \"".$adddir."\" is existed!";
        return $error;
        break;    
      }

    // 执行删除文件夹命令条
    case 4:
      $url=$_GET["url"];
      $del=$_GET["del"];
      $delfile=$url."/".$del;

      if(file_exists($delfile)){
        deldir($delfile);
        $yest="\"".$del."\" Directory deleted";
        return $yest;
        break;
      }else{
        $yest=" Sorry: The directro \"y$del\" is no exist! ";
        return $yest;
        break;
      }
  }
}
?>
 
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++网页 区+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++页头 区+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Briefness manage file system <?php echo BANQUAN; ?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <style type="text/css">
      body {
        background:#9d94ca;
      }

      a {
        text-decoration:none;
        color:red;
      }
      .jiacu {
        font-weight:bold;
        font-size:13px;
        color:#fff;
      }
    </style>
  </head>
  <!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++页头区 END+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
 
  <!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++内容 区+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  <body>
    <!-- ++++++++++++++++++++++++文件列表表格++++++++++++++++++++++++++ -->
    <center><h2>Briefness manage file system</h2></center>
    <hr width="60%">
    <!-- ++++++++++++++++++++++++显示当前操作目录++++++++++++++++++++++++++ -->
 
    <!-- ++++++++++++++++++++++++导航栏++++++++++++++++++++++++++ -->
    <div style="width:798px; height:30px; margin:20px auto 0px; border:1px solid #4c89ca;">
      <div style="width:650px; float:left; margin-left:20px; font-size:15px; font-weight:bold; line-height:30px; color:#fff;">
      <!-- ++++++++++++++++++++++++GOTO表单++++++++++++++++++++++++++ -->
 
        <form action="a.php" method="get">
        Operate Current Directroy :
        <input type="text" name="dirr" size="60" value="<?php echo $dirr; ?>" />&nbsp;&nbsp;
        <input type="submit" name="tijiao" value="goto" />
        </form>
 
      <!-- ++++++++++++++++++++++++GOTO表单END++++++++++++++++++++++++++ -->
      </div>
 
      <div style="float:right;
            line-height:22px;
            margin:5px 10px auto auto;
            text-align:center;
            width:70px; height:20px;
            font-weight:bold;
            background:#0f0;
            color:#fff;
            border-bottom:1px solid #666;
            border-right:1px solid #666;">
        <a style="color:#4a4c4a;" href="a.php?tijiao=ok&dirr=<?php echo dirname($dirr); ?>">&lt;&lt;Back</a>
      </div>
 
      <div style="clear:blod;"></div>
    </div>

    <!-- ++++++++++++++++++++++++GOTO错误提示++++++++++++++++++++++++++ --> 
    <?php
    if($adderres){
    echo "<div style=\"width:798px; height:25px; border:1px solid #9d94ca; margin:0px auto; line-height:0px; text-align:center;\">
            <p style=\"line-height:0px; color:#760E18; font-size:13px; font-weight:bold;\">$adderres</p>
        </div>";
    }
    ?>
    <!-- ++++++++++++++++++++++++GOTO错误提示END++++++++++++++++++++++++++ -->
    <!-- ++++++++++++++++++++++++导航栏END++++++++++++++++++++++++++ -->
    <!-- ++++++++++++++++++++++++显示当前操作目录END++++++++++++++++++++++++++ -->
 
    <table align="center" width="800" border="1">
      <tr>
        <th width="300">File & Directroy Name</th>
        <th>Type</th>
        <th>Size</th>
        <th>Mod-time</th>
        <th>Option</th>
      </tr>
    <?php
    // 循环遍历输出文件或文件夹目录    
    $summm=0;
    $dirmmm=0;
    $filemmm=0;
    $zongsize=0;
    if(file_exists($dirr) && is_dir($dirr)){
      if($bb=opendir($dirr)){
        while($hh=readdir($bb)){
          if($hh!="." && $hh!=".."){
            $filehaha=$dirr."/".$hh;
            $summm++;  // 统计所有对象
            if(is_dir($filehaha)){        // 文件夹则加粗显示
              echo "<tr><td style=\"font-weight:bold;\">$hh</td>";
              $dirmmm++;
            }elseif(is_file($filehaha)){
              echo "<tr><td>$hh</td>";  // 文件则正常显示
              $filemmm++;
            }

            echo "<td>".ucfirst(filetype($filehaha))."</td>";     // 文件类型

            if(is_dir($filehaha)){                // 文件夹大小
              $dani=dirsize($filehaha);
              if($dani==""){
                echo "<td>0 bytes</td>";    
              }else{
                if($dani<1024)
                  echo "<td>$dani Bytes</td>";
                elseif($dani<1024*1024)
                  echo "<td>".sprintf("%0.2f",$dani/1024)." KB</td>";
                elseif($dani<1024*1024*1024)
                  echo "<td>".sprintf("%0.2f",$dani/1024/1024)." MB</td>";
                else
                  echo "<td>".sprintf("%0.2f",$dani/1024/1024/1024)." GB</td>";

                $zongsize+=$dani;      // 累加大小
              } 

            }elseif(is_file($filehaha)){
              $danini=filesize($filehaha);        // 文件大小
              if($danini<1024)
                echo "<td>$danini Bytes</td>";
              elseif($danini<1024*1024)
                echo "<td>".sprintf("%0.2f",$danini/1024)." KB</td>";
              elseif($danini<1024*1024*1024)
                echo "<td>".sprintf("%0.2f",$danini/1024/1024)." MB</td>";
              else
                echo "<td>".sprintf("%0.2f",$danini/1024/1024/1024)." GB</td>";
              $zongsize+=$danini;

            }

            echo "<td>".date("Y-m-d H:i",filemtime($filehaha))."</td>";  // 打印日期

            // 操作选项---------------------------------------------------------------
            if(is_dir($filehaha)){   // 文件夹操作
              echo "<td><a href='a.php?tijiao=ok&subb=4&del={$hh}&url={$dirr}'>&nbsp;DEL</a>";
              echo "<a href='a.php?tijiao=ok&info={$filehaha}'>&nbsp;<font color='#00FFFF'>INTO</font></a></td></tr>";
            }elseif(is_file($filehaha)){   // 文件操作
              echo "<td><a href='a.php?tijiao=ok&subb=2&del={$hh}&url={$dirr}'>&nbsp;DEL</a>";
              // 将判断文件类型进行显示操作按钮--------------------------------------
              if(preg_match("/\.(txt|php|asp|log|ini|css|js|htm|html)$/i",$filehaha)){
                echo "<a href='a.php?tijiao=ok&edit={$hh}&url={$dirr}'>&nbsp;<font color='#0000FF'>EDIT</font></a></td></tr>";
              }elseif(preg_match("/\.(jpeg|gif|ico|png|jpg|bmp)$/i",$filehaha)){
                echo "<a href='a.php?tijiao=ok&viw={$hh}&url={$dirr}'>&nbsp;<font color='#FFFF00'>LOOK</font></a></td></tr>";
              }else{
                echo "&nbsp;<font color='#cccccc'>UNOP</font></td></tr>";
              }
            }
          }
        }
      }
    }
    ?>
    </table>
    <!-- +++++++++++++++++++++++++++++++++++文件列表表格END++++++++++++++++++++++++++++++ -->
 
    <!-- +++++++++++++++++++++++++++++++++++状态栏++++++++++++++++++++++++++++++ -->
 
    <div style="width:798px; margin:20px auto 20px; height:40px; border:1px dashed #00f; line-height:50px;">
      <span style="
        position:relative;
        display:block;
        color:#fff;
        font-size:13px;
        text-align:center;
        line-height:25px;
        font-weight:bold;
        width:250px;
        height:20px;
        background:#9d94ca;
        top:-15px; left:20px;">State and File-Directroy Tole sum:</span>
 
      <div style="
        position:relative;
        top:-40px;
        width:790px;
        text-align:left;
        margin:20px auto 0px;
        height:40px;
        font-weight:bold;
        font-size:13px;
        color:#0f0;">
        &nbsp;&nbsp;
 
        <?php // 状态栏判断显示
        if($summm==0){   // 判断如果对象总数为零
          echo " The directroy is empty! ";  // 显示文件夹为空
        }else{  
          echo " Object $summm <font color='white'> | </font> ";     // 显示对象总数
          if($dirmmm==0)                                              // 显示文件夹总数
            echo " Dir No <font color='white'> | </font>";
          else
            echo " Dir $dirmmm <font color='white'> | </font>";
          if($filemmm==0)                                               // 显示文件总数
            echo " Dir No <font color='white'> | </font> ";
          else
            echo " File $filemmm  <font color='white'> | </font> ";

          if($zongsize<1024){                                        // 显示总大小
            echo " Allsize $zongsize bytes";
          }elseif($zongsize<1024*1024){
            echo " Allsize ".sprintf("%0.2f",$zongsize/1024)." KB";
          }elseif($zongsize<1024*1024*1024){
            echo " Allsize ".sprintf("%0.2f",$zongsize/1024/1024)." MB";
          }else{
            echo " Allsize ".sprintf("%0.2f",$zongsize/1024/1024/1024)." GB";
          }
          
          // 显示磁盘剩余空间 以及总容量
          echo "<div style=\"float:right; width:280px; height:40px;\">";
          echo " Diskfree ".sprintf("%0.2f",disk_free_space($dirr)/1024/1024/1024)." G <font color='white'> | </font>";
          echo " Diskspace ".sprintf("%0.2f",disk_total_space($dirr)/1024/1024/1024)." G";
          echo "</div>";
        } 
        ?>
      </div>
      <div style="clear:both;"></div>
      <!-- +++++++++++++++++++++++++++++++++++状态栏END++++++++++++++++++++++++++++++ -->
      
      <!-- +++++++++++++++++++消息提示++++++++++++++++++++++++++ -->
      <center><font color="blue"><?php echo $yest; ?></font></center>
      <!-- ++++++++++++++++++++消息提示END++++++++++++++++++++++ -->
      <br />
      <!-- +++++++++++++++++++++++++++++++++++添加文件或文件夹表格+++++++++++++++++++++++++ -->
      <table align="center" border="0" width="500">
      <!-- +++++++++++++++添加文件表单域+++++++++++++ -->
        <form action="a.php" method="get">
          <input type="hidden" name="subb" value="1" />
          <input type="hidden" name="url" value="<?php echo $dirr; ?>" />
          <tr>
            <td width="450" align="right">AddFile:&nbsp;<input type="text" name="add" size="50" value="Please improt filename" /></td>
            <td><input type="submit" name="tijiao" value="OK" /></td>
          </tr>
        </form>
        <!-- ++++++++++++++++添加文件表单域END++++++++++++++++++++++++++++++++++++ -->

        <!-- +++++++++++++++++++添加文件夹表单域++++++++++++++++++++++++++++++ -->
        <form action="a.php" method="get">
          <input type="hidden" name="subb" value="3" />
          <input type="hidden" name="url" value="<?php echo $dirr; ?>" />
          <tr>
            <td width="450" align="right">AddDirectory:&nbsp;
            <input type="text" name="adddir" size="50" value="Please improt directroyname" /></td>
            <td><input type="submit" name="tijiao" value="OK" /></td>
          </tr>
        </form>
        <!-- ++++++++++++++++++++++添加文件夹表单域END+++++++++++++++++++++ -->
      </table>
      <!-- +++++++++++++++++++++++++++++++++++添加文件或文件夹表格END++++++++++++++++++++++++++++++++++++ -->
 
      <!-- +++++++++++++++++++消息提示++++++++++++++++++++++++++ -->    
 
      <center><font color="red"><?php echo $error; ?></font></center>
      <br />
      <!-- ++++++++++++++++++++消息提示END++++++++++++++++++++++ -->
      <!-- ++++++++++++++++++++++++++++++++编辑页面区+++++++++++++++++++++++++++++++++++++ -->
      <?php
        if($editfile){
          $duquwenben=file_get_contents($editfile);
          echo "<table width=500 align=center border=1><form action=a.php method=post>";
          echo "<tr><td>Editing: $edit </td></tr>";
          echo "<tr><td height=300><textarea name=newconten rows=20 cols=100>".htmlspecialchars($duquwenben)."</textarea></td></tr>";
          echo "<input type=hidden name=editsave value=$editfile />";
          echo "<tr><td align=center><input type=submit name=tijiao value=Save /><input type=reset value=reset /></td></tr>";
          echo "</form></table>";
        }
        /******************查看照片区*******************************/
        if($phototo){
          echo "<div style=\"weight:798px; min-height:200px; border:1px solid #ccc; text-align:center;\">";
          echo "<a style='margin:auto;' href='a.php?tijiao=photo&url=$dirr' title='Click Off'><img src=\"file:///{$phototo}\" /></a>";
          echo "<div style=\"weight:798px; min-height:20px; text-align:center; vertical-align:middle;\">$viw</div>";
        }
      ?>
      <!-- ++++++++++++++++++++++++++++++++编辑页面区END+++++++++++++++++++++++++++++++++++++ -->
 
      <!-- ++++++++++++++++++++++++++++++++++++++页脚区++++++++++++++++++++++++++++++++++-->
      <div style="width:900px;
        margin:60px auto;
        text-align:center;
        line-height:40px;
        color:#666;
        border-top:2px solid #666;
        font-size:15px;
        height:40px;
        font-weight:bold;">
            Power by qttc.net &copy; 2011
      </div>
  </body>
</html>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++内容 区+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
分享

TITLE: PHP文件管理系统代码共享

LINK: https://www.qttc.net/43-php-files-manage-system.html

NOTE: 原创内容,转载请注明出自琼台博客