溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php如何開(kāi)發(fā)一個(gè)文件管理系統(tǒng)及代碼

發(fā)布時(shí)間:2021-10-15 10:23:44 來(lái)源:億速云 閱讀:165 作者:柒染 欄目:編程語(yǔ)言

本篇文章為大家展示了php如何開(kāi)發(fā)一個(gè)文件管理系統(tǒng)及代碼,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

效果圖

<?php
//讀取管理項(xiàng)目,并且展示
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>");
}
//根據(jù)不同請(qǐng)求完成不同操作
switch($act){
  case 'createDir':
  // echo $dirName;exit;
  $res=create_dir($path.DIRECTORY_SEPARATOR.$dirName);
  if($res===true){
    $result['msg']=basename($dirName).'創(chuàng)建成功';
    $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">切換導(dǎo)航</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>首頁(yè)</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>系統(tǒng)信息</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">
                    <h2>
                        WEB在線文件管理器                    </h2>
                    <p>
              WEB在線文件管理器主要是用于管理項(xiàng)目文件,實(shí)現(xiàn)在線編輯、修改、刪除等操作。                    </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>
                                讀/寫/執(zhí)行                            </th>
                            <th>
                                訪問(wèn)時(shí)間                            </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'>打開(kāi)</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'>復(fù)制</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'>復(fù)制</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>

目錄函數(shù)文件 dir.func.php

<?php/**
 * 讀取目錄下的信息返回
 * @method read_directory
 * @param  string         $path 目標(biāo)目錄
 * @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;
}/**
 * 創(chuàng)建目錄
 * @method create_dir
 * @param  string     $path 目錄名稱
 * @return mixed          true|string */function create_dir(string $path){  if(is_dir($path)){    return $path.'當(dāng)前目錄已存在同名文件';
  }  if(!mkdir($path,755,true)){    return $path.'目錄創(chuàng)建失敗';
  }  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 '當(dāng)前目錄下存在同名文件';
  }  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;
}

文件函數(shù) file.func.php

<?php/**
 * 創(chuàng)建文件
 * @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 '當(dāng)前目錄下存在同名文件';
  }  $ext=strtolower(pathinfo($fileName,PATHINFO_EXTENSION));  if(!in_array($ext,$allowExt)){    return '非法文件類型';
  }  if(!touch($fileName)){    return '文件創(chuàng)建失敗';
  }  return true;
}/**
 * 查看文件內(nèi)容
 * @method show_contents
 * @param  string        $fileName 文件名稱
 * @param  array         $allowExt 允許的類型
 * @return string                  文件內(nèi)容 */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 '非法文件類型';
  }  //檢測(cè)是否是真實(shí)圖片
  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='文件中沒(méi)有內(nèi)容';
    }
  }  return $res;
}

上述內(nèi)容就是php如何開(kāi)發(fā)一個(gè)文件管理系統(tǒng)及代碼,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI