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

资源魔 18 0

成果图

<?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开发教程 php开发资料 php开发自学 文件管理

抱歉,评论功能暂时关闭!