当前位置:首页 > 综合实例 > 列表

php开发一个文件管理系统(附代码)

发布:smiling 来源: PHP粉丝网  添加日期:2022-05-23 09:32:03 浏览: 评论:0 

最近接到的一个项目,需要单独管理文件,这就类似于虚拟服务器文件管理系统一样,不需要其它多余的功能,只要能实现简单的文件管理就可以了,然后从网上搜到一个很实用的文件管理系统,如果有需要的可以直接粘贴使用,效果图如下:

php开发一个文件管理系统(附代码)

  1. <?php 
  2.  
  3. //读取管理项目,并且展示 
  4.  
  5. require_once 'lib/dir.func.php'; 
  6.  
  7. require_once 'lib/file.func.php'; 
  8.  
  9. date_default_timezone_set("PRC"); 
  10.  
  11. error_reporting(E_ALL&~E_NOTICE); 
  12.  
  13. define('WEBROOT','webRoot'); 
  14.  
  15. $path=$_REQUEST['path']?$_REQUEST['path']:WEBROOT; 
  16.  
  17. $act=$_REQUEST['act']?$_REQUEST['act']:''; 
  18.  
  19. $dirName=$_REQUEST['dirName']?$_REQUEST['dirName']:''; 
  20.  
  21. $fileName=$_REQUEST['fileName']?$_REQUEST['fileName']:''; 
  22.  
  23. $info=read_directory($path); 
  24.  
  25. // print_r($info);exit; 
  26.  
  27. if(!is_array($info)){ 
  28.  
  29.   exit("<script> 
  30.  
  31.   alert('读取失败'); 
  32.  
  33.   location.href='index.php'; 
  34.  
  35.   </script>"); 
  36.  
  37. } 
  38.  
  39. //根据不同请求完成不同操作 
  40.  
  41. switch($act){ 
  42.  
  43.   case 'createDir': 
  44.  
  45.   // echo $dirName;exit; 
  46.  
  47.   $res=create_dir($path.DIRECTORY_SEPARATOR.$dirName); 
  48.  
  49.   if($res===true){ 
  50.  
  51.     $result['msg']=basename($dirName).'创建成功'; 
  52.  
  53.     $result['icon']=1; 
  54.  
  55.   }else{ 
  56.  
  57.     $result['msg']=$res; 
  58.  
  59.     $result['icon']=2; 
  60.  
  61.   } 
  62.  
  63.   exit(json_encode($result)); 
  64.  
  65.   break; 
  66.  
  67.   case 'renameDir': 
  68.  
  69.   $newName=$path.DIRECTORY_SEPARATOR.$dirName; 
  70.  
  71.   $res=rename_dir($fileName,$newName); 
  72.  
  73.   if($res===true){ 
  74.  
  75.     $result['msg']=$fileName.'重命名成功'; 
  76.  
  77.     $result['icon']=1; 
  78.  
  79.   }else{ 
  80.  
  81.     $result['msg']=$res; 
  82.  
  83.     $result['icon']=2; 
  84.  
  85.   } 
  86.  
  87.   exit(json_encode($result)); 
  88.  
  89.   break; 
  90.  
  91.   case 'delDir': 
  92.  
  93.   $res=del_dir($fileName); 
  94.  
  95.   if($res===true){ 
  96.  
  97.     $result['msg']=basename($fileName).'删除成功'; 
  98.  
  99.     $result['icon']=1; 
  100.  
  101.   }else{ 
  102.  
  103.     $result['msg']=$res; 
  104.  
  105.     $result['icon']=2; 
  106.  
  107.   } 
  108.  
  109.   exit(json_encode($result)); 
  110.  
  111.   break; 
  112.  
  113.   //文件部分 
  114.  
  115.   case 'createFile': 
  116.  
  117.   $res=create_file($path.DIRECTORY_SEPARATOR.$fileName); 
  118.  
  119.   if($res===true){ 
  120.  
  121.     $result['msg']=basename($fileName).'文件新建成功'; 
  122.  
  123.     $result['icon']=1; 
  124.  
  125.   }else{ 
  126.  
  127.     $result['msg']=$res; 
  128.  
  129.     $result['icon']=2; 
  130.  
  131.   } 
  132.  
  133.   exit(json_encode($result)); 
  134.  
  135.   break; 
  136.  
  137.   case 'showContents': 
  138.  
  139.   $res=show_contents($fileName); 
  140.  
  141.   exit($res); 
  142.  
  143.   break; 
  144.  
  145. }?><!DOCTYPE html><html lang="zh-cn"> 
  146.  
  147.   <head> 
  148.  
  149.     <meta charset="utf-8"> 
  150.  
  151.     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
  152.  
  153.     <meta name="viewport" content="width=device-width, initial-scale=1"> 
  154.  
  155.     <title>WEB在线文件管理器</title> 
  156.  
  157.     <!-- Bootstrap --> 
  158.  
  159.     <link href="css/bootstrap.min.css" rel="stylesheet"> 
  160.  
  161.  
  162.  
  163.     <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> 
  164.  
  165.     <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 
  166.  
  167.     <!--[if lt IE 9]> 
  168.  
  169.       <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script> 
  170.  
  171.       <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script> 
  172.  
  173.     <![endif]--> 
  174.  
  175.   </head> 
  176.  
  177.   <body> 
  178.  
  179.     <p class="container"> 
  180.  
  181.         <p class="row clearfix"> 
  182.  
  183.             <p class="col-md-12 column"> 
  184.  
  185.                 <nav class="navbar navbar-default" role="navigation"> 
  186.  
  187.                     <p class="navbar-header"> 
  188.  
  189.                          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">切换导航</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button> <a  class="navbar-brand" href="index.php"><span class="glyphicon glyphicon-home"></span>首页</a> 
  190.  
  191.                     </p> 
  192.  
  193.  
  194.  
  195.                     <p class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
  196.  
  197.                         <ul class="nav navbar-nav"> 
  198.  
  199.                             <li class="active"> 
  200.  
  201.                                 <a href="javascript:void(0)" class='createDir' data-url="index.php?act=createDir&path=<?php echo $path;?>"><span class="glyphicon glyphicon-folder-open"></span>新建目录</a> 
  202.  
  203.                             </li> 
  204.  
  205.                             <li> 
  206.  
  207.                   <a href="javascript:void(0)" class="createFile" data-url="index.php?act=createFile&path=<?php echo $path;?>"><span class="glyphicon glyphicon-file"></span>新建文件</a> 
  208.  
  209.                             </li> 
  210.  
  211.                 <li> 
  212.  
  213.                                 <a  href="#"><span class="glyphicon glyphicon-upload"></span>上传文件</a> 
  214.  
  215.                             </li> 
  216.  
  217.                 <li> 
  218.  
  219.                                 <a  href="#"><span class="glyphicon glyphicon-info-sign"></span>系统信息</a> 
  220.  
  221.                             </li> 
  222.  
  223.                                 </ul> 
  224.  
  225.                             </li> 
  226.  
  227.                         </ul> 
  228.  
  229.                         <form class="navbar-form navbar-left" role="search"> 
  230.  
  231.                             <p class="form-group"> 
  232.  
  233.                                 <input type="text" class="form-control" /> 
  234.  
  235.                             </p> <button type="submit" class="btn btn-default">搜索</button> 
  236.  
  237.                         </form> 
  238.  
  239.                     </p> 
  240.  
  241.  
  242.  
  243.                 </nav> 
  244.  
  245.                 <p class="jumbotron nofollow"> 
  246.  
  247.                     <h1> 
  248.  
  249.                         WEB在线文件管理器                    </h1> 
  250.  
  251.                     <p> 
  252.  
  253.               WEB在线文件管理器主要是用于管理项目文件,实现在线编辑、修改、删除等操作。                    </p> 
  254.  
  255.                     <p> 
  256.  
  257.                         <a  class="btn btn-primary btn-large" href="#">查看更多 »</a> 
  258.  
  259.                     </p> 
  260.  
  261.                 </p> 
  262.  
  263.                 <table class="table table-bordered table-hover table-condensed"> 
  264.  
  265.                     <thead> 
  266.  
  267.                         <tr> 
  268.  
  269.                             <th> 
  270.  
  271.                                 类型                            </th> 
  272.  
  273.                             <th> 
  274.  
  275.                                 名称                            </th> 
  276.  
  277.                             <th> 
  278.  
  279.                                 读/写/执行                            </th> 
  280.  
  281.                             <th> 
  282.  
  283.                                 访问时间                            </th> 
  284.  
  285.                 <th> 
  286.  
  287.                   操作                </th> 
  288.  
  289.                         </tr> 
  290.  
  291.                     </thead> 
  292.  
  293.                     <tbody> 
  294.  
  295.               <!-- 目录部分 --> 
  296.  
  297.               <?php 
  298.  
  299.               if(is_array($info['dir'])){ 
  300.  
  301.                 foreach($info['dir'] as $val){              ?> 
  302.  
  303.               <tr class="success"> 
  304.  
  305.                 <td><span class="glyphicon glyphicon-folder-close"></span></td> 
  306.  
  307.                 <td><?php echo $val['showName'];?></td> 
  308.  
  309.                 <td> 
  310.  
  311.                   <span class="glyphicon <?php echo $val['readable']?'glyphicon-ok':'glyphicon-remove';?>"></span> 
  312.  
  313.                   <span class="glyphicon <?php echo $val['writable']?'glyphicon-ok':'glyphicon-remove';?>"></span> 
  314.  
  315.                   <span class="glyphicon <?php echo $val['executable']?'glyphicon-ok':'glyphicon-remove';?>"></span> 
  316.  
  317.                 </td> 
  318.  
  319.                 <td><?php echo $val['atime'];?></td> 
  320.  
  321.                 <td> 
  322.  
  323.                   <a href="index.php?path=<?php echo $val['fileName'];?>" class='btn btn-primary btn-sm'>打开</a> 
  324.  
  325.                   <a href="javascript:void(0)"  class='btn btn-primary btn-sm renameDir' data-url='index.php?act=renameDir&fileName=<?php echo $val['fileName'];?>&path=<?php echo $path;?>' data-showName='<?php echo $val['showName'];?>'>重命名</a> 
  326.  
  327.                   <a href="#" class='btn btn-primary btn-sm'>剪切</a> 
  328.  
  329.                   <a href="#" class='btn btn-primary btn-sm'>复制</a> 
  330.  
  331.                   <a href="javascript:void(0)" class='btn btn-danger btn-sm delDir' data-url='index.php?act=delDir&fileName=<?php echo $val['fileName'];?>&path=<?php echo $path;?>' data-showName='<?php echo $val['showName'];?>'>删除</a> 
  332.  
  333.                 </td> 
  334.  
  335.               </tr> 
  336.  
  337.               <?php 
  338.  
  339.                 } 
  340.  
  341.               }              ?> 
  342.  
  343.  
  344.  
  345.               <!-- 文件部分 --> 
  346.  
  347.               <?php 
  348.  
  349.               if(is_array($info['file'])){ 
  350.  
  351.                 foreach($info['file'] as $val){              ?> 
  352.  
  353.               <tr class="warning"> 
  354.  
  355.                 <td><span class="glyphicon glyphicon-file"></span></td> 
  356.  
  357.                 <td><?php echo $val['showName'];?></td> 
  358.  
  359.                 <td> 
  360.  
  361.                   <span class="glyphicon <?php echo $val['readable']?'glyphicon-ok':'glyphicon-remove';?>"></span> 
  362.  
  363.                   <span class="glyphicon <?php echo $val['writable']?'glyphicon-ok':'glyphicon-remove';?>"></span> 
  364.  
  365.                   <span class="glyphicon <?php echo $val['executable']?'glyphicon-ok':'glyphicon-remove';?>"></span> 
  366.  
  367.                 </td> 
  368.  
  369.                 <td><?php echo $val['atime'];?></td> 
  370.  
  371.                 <td> 
  372.  
  373.                   <a href="#" class='btn btn-primary btn-sm showContents' data-url="index.php?act=showContents&fileName=<?php echo $val['fileName'];?>&path=<?php echo $path;?>">查看</a> 
  374.  
  375.                   <a href="#" class='btn btn-primary btn-sm'>编辑</a> 
  376.  
  377.                   <a href="#" class='btn btn-primary btn-sm'>下载</a> 
  378.  
  379.                   <a href="#" class='btn btn-primary btn-sm'>重命名</a> 
  380.  
  381.                   <a href="#" class='btn btn-primary btn-sm'>剪切</a> 
  382.  
  383.                   <a href="#" class='btn btn-primary btn-sm'>复制</a> 
  384.  
  385.                   <a href="#" class='btn btn-danger btn-sm'>删除</a> 
  386.  
  387.                 </td> 
  388.  
  389.               </tr> 
  390.  
  391.               <?php 
  392.  
  393.                 } 
  394.  
  395.               }              ?> 
  396.  
  397.                     </tbody> 
  398.  
  399.                 </table> 
  400.  
  401.             </p> 
  402.  
  403.         </p> 
  404.  
  405.     </p> 
  406.  
  407.  
  408.  
  409.  
  410.  
  411.     <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
  412.  
  413.     <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script> 
  414.  
  415.     <!-- Include all compiled plugins (below), or include inpidual files as needed --> 
  416.  
  417.     <script src="js/bootstrap.min.js"></script> 
  418.  
  419.     <script src="layer/layer.js"></script> 
  420.  
  421.     <script src="js/dir.js"></script> 
  422.  
  423.     <script src="js/file.js"></script> 
  424.  
  425.   </body></html> 

目录函数文件 dir.func.php

  1. <?php/** 
  2.  
  3.  * 读取目录下的信息返回 
  4.  
  5.  * @method read_directory 
  6.  
  7.  * @param  string         $path 目标目录 
  8.  
  9.  * @return mixed               false|array */function read_directory(string $path){  if(!is_dir($path)){    return false; 
  10.  
  11.   }  $info=[];  $handle=opendir($path);  while(($item=@readdir($handle))!==false){    if($item!='.'&&$item!='..'){      $filePath=$path.DIRECTORY_SEPARATOR.$item;      $info['fileName']=$filePath;      $info['showName']=$item;      $info['readable']=is_readable($filePath)?true:false;      $info['writable']=is_writable($filePath)?true:false;      $info['executable']=is_executable($filePath)?true:false;      $info['atime']=date('Y/m/d H:i:s',fileatime($filePath));      if(is_file($filePath)){        $arr['file'][]=$info; 
  12.  
  13.       }      if(is_dir($filePath)){        $arr['dir'][]=$info; 
  14.  
  15.       } 
  16.  
  17.     } 
  18.  
  19.   }  closedir($handle);  return $arr; 
  20.  
  21. }/** 
  22.  
  23.  * 创建目录 
  24.  
  25.  * @method create_dir 
  26.  
  27.  * @param  string     $path 目录名称 
  28.  
  29.  * @return mixed          true|string */function create_dir(string $path){  if(is_dir($path)){    return $path.'当前目录已存在同名文件'; 
  30.  
  31.   }  if(!mkdir($path,755,true)){    return $path.'目录创建失败'; 
  32.  
  33.   }  return true; 
  34.  
  35. }/** 
  36.  
  37.  * 重命名目录 
  38.  
  39.  * @method rename_dir 
  40.  
  41.  * @param  string     $oldName 原目录 
  42.  
  43.  * @param  string     $newName 新名称 
  44.  
  45.  * @return mixed              string|true */function rename_dir(string $oldName,string $newName){  if(!is_dir($oldName)){    return '原目录不存在'; 
  46.  
  47.   }  if(is_dir($newName)){    return '当前目录下存在同名文件'; 
  48.  
  49.   }  if(!rename($oldName,$newName)){    return '重命名失败'; 
  50.  
  51.   }  return true; 
  52.  
  53. }/** 
  54.  
  55.  * 删除目录 
  56.  
  57.  * @method del_dir 
  58.  
  59.  * @param  string  $path 目录名称 
  60.  
  61.  * @return mixed        true|string */function del_dir(string $path){  if(!is_dir($path)){    return '目录不存在'; 
  62.  
  63.   }  $handle=opendir($path);  while(($item=@readdir($handle))!==false){    if($item!='.'&&$item!='..'){      $pathName=$path.DIRECTORY_SEPARATOR.$item;      if(is_file($pathName)){ 
  64.  
  65.         @unlink($pathName); 
  66.  
  67.       }      if(is_dir($pathName)){        $func=__FUNCTION__;        $func($pathName); 
  68.  
  69.       } 
  70.  
  71.     } 
  72.  
  73.   }  closedir($handle);  rmdir($path);  return true; 
  74.  
  75. } 

文件函数 file.func.php

  1. <?php/** 
  2.  
  3.  * 创建文件 
  4.  
  5.  * @method create_file 
  6.  
  7.  * @param  string      $fileName 文件名称 
  8.  
  9.  * @param  array       $allowExt 允许的文件类型 
  10.  
  11.  * @return mixed                true|string */function create_file(string $fileName,$allowExt=array('txt','html','php')){  if(is_file($fileName)){    return '当前目录下存在同名文件'; 
  12.  
  13.   }  $ext=strtolower(pathinfo($fileName,PATHINFO_EXTENSION));  if(!in_array($ext,$allowExt)){    return '非法文件类型'; 
  14.  
  15.   }  if(!touch($fileName)){    return '文件创建失败'; 
  16.  
  17.   }  return true; 
  18.  
  19. }/** 
  20.  
  21.  * 查看文件内容 
  22.  
  23.  * @method show_contents 
  24.  
  25.  * @param  string        $fileName 文件名称 
  26.  
  27.  * @param  array         $allowExt 允许的类型 
  28.  
  29.  * @return string                  文件内容 */function show_contents(string $fileName,$allowExt=array('jpg','jpeg','png','gif','txt','html','php')){  if(!is_file($fileName)){    return '文件不存在'; 
  30.  
  31.   }  $ext=strtolower(pathinfo($fileName,PATHINFO_EXTENSION));  if(!in_array($ext,$allowExt)){    return '非法文件类型'; 
  32.  
  33.   }  //检测是否是真实图片 
  34.  
  35.   if(getimagesize($fileName)){    $res="<img src='{$fileName}' class='img-responsive'/>"; 
  36.  
  37.   }else{    $str=file_get_contents($fileName);    if(strlen($str)>0){      $res=highlight_string($str,true); 
  38.  
  39.     }else{      $res='文件中没有内容'; 
  40.  
  41.     } 
  42.  
  43.   }  return $res; 
  44.  
  45. }

Tags: php文件管理系统

分享到: