侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

php  /  管理员 发布于 7年前   263

效果图

<?php//读取管理项目,并且展示require_once 'lib/dir.func.php';require_once 'lib/file.func.php';date_default_timezone_set("PRC");error_reporting(E_ALL&~E_NOTICE);define('WEBROOT','webRoot');$path=$_REQUEST['path']?$_REQUEST['path']:WEBROOT;$act=$_REQUEST['act']?$_REQUEST['act']:'';$dirName=$_REQUEST['dirName']?$_REQUEST['dirName']:'';$fileName=$_REQUEST['fileName']?$_REQUEST['fileName']:'';$info=read_directory($path);// print_r($info);exit;if(!is_array($info)){  exit("<script>  alert('读取失败');  location.href='index.php';  </script>");}//根据不同请求完成不同操作switch($act){  case 'createDir':  // echo $dirName;exit;  $res=create_dir($path.DIRECTORY_SEPARATOR.$dirName);  if($res===true){    $result['msg']=basename($dirName).'创建成功';    $result['icon']=1;  }else{    $result['msg']=$res;    $result['icon']=2;  }  exit(json_encode($result));  break;  case 'renameDir':  $newName=$path.DIRECTORY_SEPARATOR.$dirName;  $res=rename_dir($fileName,$newName);  if($res===true){    $result['msg']=$fileName.'重命名成功';    $result['icon']=1;  }else{    $result['msg']=$res;    $result['icon']=2;  }  exit(json_encode($result));  break;  case 'delDir':  $res=del_dir($fileName);  if($res===true){    $result['msg']=basename($fileName).'删除成功';    $result['icon']=1;  }else{    $result['msg']=$res;    $result['icon']=2;  }  exit(json_encode($result));  break;  //文件部分  case 'createFile':  $res=create_file($path.DIRECTORY_SEPARATOR.$fileName);  if($res===true){    $result['msg']=basename($fileName).'文件新建成功';    $result['icon']=1;  }else{    $result['msg']=$res;    $result['icon']=2;  }  exit(json_encode($result));  break;  case 'showContents':  $res=show_contents($fileName);  exit($res);  break;}?><!DOCTYPE html><html lang="zh-cn">  <head>    <meta charset="utf-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1">    <title>WEB在线文件管理器</title>    <!-- Bootstrap -->    <link href="css/bootstrap.min.css" rel="stylesheet">    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->    <!--[if lt IE 9]>      <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>      <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>    <![endif]-->  </head>  <body>    <p class="container">        <p class="row clearfix"><p class="col-md-12 column">    <nav class="navbar navbar-default" role="navigation">        <p class="navbar-header"> <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>        </p>        <p class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"><ul class="nav navbar-nav">    <li class="active">        <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>    </li>    <li>      <a href="javascript:void(0)" class="createFile" data-url="index.php?act=createFile&path=<?php echo $path;?>"><span class="glyphicon glyphicon-file"></span>新建文件</a>    </li>    <li>        <a  href="#"><span class="glyphicon glyphicon-upload"></span>上传文件</a>    </li>    <li>        <a  href="#"><span class="glyphicon glyphicon-info-sign"></span>系统信息</a>    </li>        </ul>    </li></ul><form class="navbar-form navbar-left" role="search">    <p class="form-group">        <input type="text" class="form-control" />    </p> <button type="submit" class="btn btn-default">搜索</button></form>        </p>    </nav>    <p class="jumbotron nofollow">        <h1>WEB在线文件管理器        </h1>        <p>  WEB在线文件管理器主要是用于管理项目文件,实现在线编辑、修改、删除等操作。        </p>        <p><a  class="btn btn-primary btn-large" href="#">查看更多 »</a>        </p>    </p>    <table class="table table-bordered table-hover table-condensed">        <thead><tr>    <th>        类型    </th>    <th>        名称    </th>    <th>        读/写/执行    </th>    <th>        访问时间    </th>    <th>      操作    </th></tr>        </thead>        <tbody>  <!-- 目录部分 -->  <?php  if(is_array($info['dir'])){    foreach($info['dir'] as $val){  ?>  <tr class="success">    <td><span class="glyphicon glyphicon-folder-close"></span></td>    <td><?php echo $val['showName'];?></td>    <td>      <span class="glyphicon <?php echo $val['readable']?'glyphicon-ok':'glyphicon-remove';?>"></span>      <span class="glyphicon <?php echo $val['writable']?'glyphicon-ok':'glyphicon-remove';?>"></span>      <span class="glyphicon <?php echo $val['executable']?'glyphicon-ok':'glyphicon-remove';?>"></span>    </td>    <td><?php echo $val['atime'];?></td>    <td>      <a href="index.php?path=<?php echo $val['fileName'];?>" class='btn btn-primary btn-sm'>打开</a>      <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>      <a href="#" class='btn btn-primary btn-sm'>剪切</a>      <a href="#" class='btn btn-primary btn-sm'>复制</a>      <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>    </td>  </tr>  <?php    }  }  ?>  <!-- 文件部分 -->  <?php  if(is_array($info['file'])){    foreach($info['file'] as $val){  ?>  <tr class="warning">    <td><span class="glyphicon glyphicon-file"></span></td>    <td><?php echo $val['showName'];?></td>    <td>      <span class="glyphicon <?php echo $val['readable']?'glyphicon-ok':'glyphicon-remove';?>"></span>      <span class="glyphicon <?php echo $val['writable']?'glyphicon-ok':'glyphicon-remove';?>"></span>      <span class="glyphicon <?php echo $val['executable']?'glyphicon-ok':'glyphicon-remove';?>"></span>    </td>    <td><?php echo $val['atime'];?></td>    <td>      <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>      <a href="#" class='btn btn-primary btn-sm'>编辑</a>      <a href="#" class='btn btn-primary btn-sm'>下载</a>      <a href="#" class='btn btn-primary btn-sm'>重命名</a>      <a href="#" class='btn btn-primary btn-sm'>剪切</a>      <a href="#" class='btn btn-primary btn-sm'>复制</a>      <a href="#" class='btn btn-danger btn-sm'>删除</a>    </td>  </tr>  <?php    }  }  ?>        </tbody>    </table></p>        </p>    </p>    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->    <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>    <!-- Include all compiled plugins (below), or include inpidual files as needed -->    <script src="js/bootstrap.min.js"></script>    <script src="layer/layer.js"></script>    <script src="js/dir.js"></script>    <script src="js/file.js"></script>  </body></html>

目录函数文件 dir.func.php

<?php/** * 读取目录下的信息返回 * @method read_directory * @param  string         $path 目标目录 * @return mixed   false|array */function read_directory(string $path){  if(!is_dir($path)){    return false;  }  $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;      }      if(is_dir($filePath)){        $arr['dir'][]=$info;      }    }  }  closedir($handle);  return $arr;}/** * 创建目录 * @method create_dir * @param  string     $path 目录名称 * @return mixed          true|string */function create_dir(string $path){  if(is_dir($path)){    return $path.'当前目录已存在同名文件';  }  if(!mkdir($path,755,true)){    return $path.'目录创建失败';  }  return true;}/** * 重命名目录 * @method rename_dir * @param  string     $oldName 原目录 * @param  string     $newName 新名称 * @return mixed  string|true */function rename_dir(string $oldName,string $newName){  if(!is_dir($oldName)){    return '原目录不存在';  }  if(is_dir($newName)){    return '当前目录下存在同名文件';  }  if(!rename($oldName,$newName)){    return '重命名失败';  }  return true;}/** * 删除目录 * @method del_dir * @param  string  $path 目录名称 * @return mixed        true|string */function del_dir(string $path){  if(!is_dir($path)){    return '目录不存在';  }  $handle=opendir($path);  while(($item=@readdir($handle))!==false){    if($item!='.'&&$item!='..'){      $pathName=$path.DIRECTORY_SEPARATOR.$item;      if(is_file($pathName)){        @unlink($pathName);      }      if(is_dir($pathName)){        $func=__FUNCTION__;        $func($pathName);      }    }  }  closedir($handle);  rmdir($path);  return true;}

文件函数 file.func.php

<?php/** * 创建文件 * @method create_file * @param  string      $fileName 文件名称 * @param  array       $allowExt 允许的文件类型 * @return mixed    true|string */function create_file(string $fileName,$allowExt=array('txt','html','php')){  if(is_file($fileName)){    return '当前目录下存在同名文件';  }  $ext=strtolower(pathinfo($fileName,PATHINFO_EXTENSION));  if(!in_array($ext,$allowExt)){    return '非法文件类型';  }  if(!touch($fileName)){    return '文件创建失败';  }  return true;}/** * 查看文件内容 * @method show_contents * @param  string        $fileName 文件名称 * @param  array         $allowExt 允许的类型 * @return string      文件内容 */function show_contents(string $fileName,$allowExt=array('jpg','jpeg','png','gif','txt','html','php')){  if(!is_file($fileName)){    return '文件不存在';  }  $ext=strtolower(pathinfo($fileName,PATHINFO_EXTENSION));  if(!in_array($ext,$allowExt)){    return '非法文件类型';  }  //检测是否是真实图片  if(getimagesize($fileName)){    $res="<img src='{$fileName}' class='img-responsive'/>";  }else{    $str=file_get_contents($fileName);    if(strlen($str)>0){      $res=highlight_string($str,true);    }else{      $res='文件中没有内容';    }  }  return $res;}

本文来自,php教程栏目,欢迎学习!

以上就是php开发一个文件管理系统(附代码)的详细内容,更多请关注其它相关文章!


  • 上一条:
    php中异步执行的四种方式
    下一条:
    php中加密解密DES的正确使用姿势
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客