Home >Backend Development >PHP Tutorial >Develop a file management system in php (with code)

Develop a file management system in php (with code)

angryTom
angryTomforward
2020-01-16 17:31:399596browse

Develop a file management system in php (with code)

Rendering

<?php
//读取管理项目,并且展示
require_once &#39;lib/dir.func.php&#39;;
require_once &#39;lib/file.func.php&#39;;
date_default_timezone_set("PRC");
error_reporting(E_ALL&~E_NOTICE);
define(&#39;WEBROOT&#39;,&#39;webRoot&#39;);
$path=$_REQUEST[&#39;path&#39;]?$_REQUEST[&#39;path&#39;]:WEBROOT;
$act=$_REQUEST[&#39;act&#39;]?$_REQUEST[&#39;act&#39;]:&#39;&#39;;
$dirName=$_REQUEST[&#39;dirName&#39;]?$_REQUEST[&#39;dirName&#39;]:&#39;&#39;;
$fileName=$_REQUEST[&#39;fileName&#39;]?$_REQUEST[&#39;fileName&#39;]:&#39;&#39;;
$info=read_directory($path);
// print_r($info);exit;
if(!is_array($info)){
  exit("<script>
  alert(&#39;读取失败&#39;);
  location.href=&#39;index.php&#39;;
  </script>");
}
//根据不同请求完成不同操作
switch($act){
  case &#39;createDir&#39;:
  // echo $dirName;exit;
  $res=create_dir($path.DIRECTORY_SEPARATOR.$dirName);
  if($res===true){
    $result[&#39;msg&#39;]=basename($dirName).&#39;创建成功&#39;;
    $result[&#39;icon&#39;]=1;
  }else{
    $result[&#39;msg&#39;]=$res;
    $result[&#39;icon&#39;]=2;
  }
  exit(json_encode($result));
  break;
  case &#39;renameDir&#39;:
  $newName=$path.DIRECTORY_SEPARATOR.$dirName;
  $res=rename_dir($fileName,$newName);
  if($res===true){
    $result[&#39;msg&#39;]=$fileName.&#39;重命名成功&#39;;
    $result[&#39;icon&#39;]=1;
  }else{
    $result[&#39;msg&#39;]=$res;
    $result[&#39;icon&#39;]=2;
  }
  exit(json_encode($result));
  break;
  case &#39;delDir&#39;:
  $res=del_dir($fileName);
  if($res===true){
    $result[&#39;msg&#39;]=basename($fileName).&#39;删除成功&#39;;
    $result[&#39;icon&#39;]=1;
  }else{
    $result[&#39;msg&#39;]=$res;
    $result[&#39;icon&#39;]=2;
  }
  exit(json_encode($result));
  break;
  //文件部分
  case &#39;createFile&#39;:
  $res=create_file($path.DIRECTORY_SEPARATOR.$fileName);
  if($res===true){
    $result[&#39;msg&#39;]=basename($fileName).&#39;文件新建成功&#39;;
    $result[&#39;icon&#39;]=1;
  }else{
    $result[&#39;msg&#39;]=$res;
    $result[&#39;icon&#39;]=2;
  }
  exit(json_encode($result));
  break;
  case &#39;showContents&#39;:
  $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&#39;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=&#39;createDir&#39; 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[&#39;dir&#39;])){
                foreach($info[&#39;dir&#39;] as $val){              ?>
              <tr class="success">
                <td><span class="glyphicon glyphicon-folder-close"></span></td>
                <td><?php echo $val[&#39;showName&#39;];?></td>
                <td>
                  <span class="glyphicon <?php echo $val[&#39;readable&#39;]?&#39;glyphicon-ok&#39;:&#39;glyphicon-remove&#39;;?>"></span>
                  <span class="glyphicon <?php echo $val[&#39;writable&#39;]?&#39;glyphicon-ok&#39;:&#39;glyphicon-remove&#39;;?>"></span>
                  <span class="glyphicon <?php echo $val[&#39;executable&#39;]?&#39;glyphicon-ok&#39;:&#39;glyphicon-remove&#39;;?>"></span>
                </td>
                <td><?php echo $val[&#39;atime&#39;];?></td>
                <td>
                  <a href="index.php?path=<?php echo $val[&#39;fileName&#39;];?>" class=&#39;btn btn-primary btn-sm&#39;>打开</a>
                  <a href="javascript:void(0)"  class=&#39;btn btn-primary btn-sm renameDir&#39; data-url=&#39;index.php?act=renameDir&fileName=<?php echo $val[&#39;fileName&#39;];?>&path=<?php echo $path;?>&#39; data-showName=&#39;<?php echo $val[&#39;showName&#39;];?>&#39;>重命名</a>
                  <a href="#" class=&#39;btn btn-primary btn-sm&#39;>剪切</a>
                  <a href="#" class=&#39;btn btn-primary btn-sm&#39;>复制</a>
                  <a href="javascript:void(0)" class=&#39;btn btn-danger btn-sm delDir&#39; data-url=&#39;index.php?act=delDir&fileName=<?php echo $val[&#39;fileName&#39;];?>&path=<?php echo $path;?>&#39; data-showName=&#39;<?php echo $val[&#39;showName&#39;];?>&#39;>删除</a>
                </td>
              </tr>
              <?php
                }
              }              ?>

              <!-- 文件部分 -->
              <?php
              if(is_array($info[&#39;file&#39;])){
                foreach($info[&#39;file&#39;] as $val){              ?>
              <tr class="warning">
                <td><span class="glyphicon glyphicon-file"></span></td>
                <td><?php echo $val[&#39;showName&#39;];?></td>
                <td>
                  <span class="glyphicon <?php echo $val[&#39;readable&#39;]?&#39;glyphicon-ok&#39;:&#39;glyphicon-remove&#39;;?>"></span>
                  <span class="glyphicon <?php echo $val[&#39;writable&#39;]?&#39;glyphicon-ok&#39;:&#39;glyphicon-remove&#39;;?>"></span>
                  <span class="glyphicon <?php echo $val[&#39;executable&#39;]?&#39;glyphicon-ok&#39;:&#39;glyphicon-remove&#39;;?>"></span>
                </td>
                <td><?php echo $val[&#39;atime&#39;];?></td>
                <td>
                  <a href="#" class=&#39;btn btn-primary btn-sm showContents&#39; data-url="index.php?act=showContents&fileName=<?php echo $val[&#39;fileName&#39;];?>&path=<?php echo $path;?>">查看</a>
                  <a href="#" class=&#39;btn btn-primary btn-sm&#39;>编辑</a>
                  <a href="#" class=&#39;btn btn-primary btn-sm&#39;>下载</a>
                  <a href="#" class=&#39;btn btn-primary btn-sm&#39;>重命名</a>
                  <a href="#" class=&#39;btn btn-primary btn-sm&#39;>剪切</a>
                  <a href="#" class=&#39;btn btn-primary btn-sm&#39;>复制</a>
                  <a href="#" class=&#39;btn btn-danger btn-sm&#39;>删除</a>
                </td>
              </tr>
              <?php
                }
              }              ?>
                    </tbody>
                </table>
            </p>
        </p>
    </p>


    <!-- jQuery (necessary for Bootstrap&#39;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>

Directory function file 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!=&#39;.&#39;&&$item!=&#39;..&#39;){      $filePath=$path.DIRECTORY_SEPARATOR.$item;      $info[&#39;fileName&#39;]=$filePath;      $info[&#39;showName&#39;]=$item;      $info[&#39;readable&#39;]=is_readable($filePath)?true:false;      $info[&#39;writable&#39;]=is_writable($filePath)?true:false;      $info[&#39;executable&#39;]=is_executable($filePath)?true:false;      $info[&#39;atime&#39;]=date(&#39;Y/m/d H:i:s&#39;,fileatime($filePath));      if(is_file($filePath)){        $arr[&#39;file&#39;][]=$info;
      }      if(is_dir($filePath)){        $arr[&#39;dir&#39;][]=$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.&#39;当前目录已存在同名文件&#39;;
  }  if(!mkdir($path,755,true)){    return $path.&#39;目录创建失败&#39;;
  }  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 &#39;原目录不存在&#39;;
  }  if(is_dir($newName)){    return &#39;当前目录下存在同名文件&#39;;
  }  if(!rename($oldName,$newName)){    return &#39;重命名失败&#39;;
  }  return true;
}/**
 * 删除目录
 * @method del_dir
 * @param  string  $path 目录名称
 * @return mixed        true|string */function del_dir(string $path){  if(!is_dir($path)){    return &#39;目录不存在&#39;;
  }  $handle=opendir($path);  while(($item=@readdir($handle))!==false){    if($item!=&#39;.&#39;&&$item!=&#39;..&#39;){      $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 function file.func.php

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

This article comes from the php Chinese website, php tutorial column, welcome to learn!

The above is the detailed content of Develop a file management system in php (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete