<html>
<!--此HTML文见提供一个上传文件的表单 -->
<!--系统入口-->
<!--提交之后会自动跳到list表单-->
<head>
<meta charset="utf-8">
</head>
<body>
<!--文件跳转到主显示界面 getFile.php-->
<form action="getFile.php" method="GET">
输入路径: <input type="text" name="dir"/>
<input type="submit" value="start"/>
</form>
</body>
</html>
<html>
<!--会自动检索出first.html所提交的路径-->
<!--主页面 同时提供一个查的功能-->
<!--通过嵌套PHP代码 使浏览器显示出PHP处理出来的数据-->
<head>
<meta charset="utf-8">
</head>
<body>
<!--制作一个表格 -->
<table border="1">
<p style="align-content: center" ><?php echo $dir;?>目录下的文件</p>
<a href="first.html" 回到主页></a>
<tr>
<th>序号</th>
<th>文件名称</th>
<th>文件类型</th>
<th>操作</th>
</tr>
<tr>
<!--文件本身没有新增功能-->
<!--解决序号问题 在循环外初始化 1 -->
<?php $count =1;?>
<!--遍历二维数组 每一次序号++ 调用html表格代码 把信息按照html格式显示出来-->
<?php foreach($res as $files): ?>
<!--由于每个文件夹下面都有 . .. 文件夹 没意义 过滤掉-->
<?php if($files['fileName']=='.'|| $files['fileName']=='..') continue; ?>
<!--通过echo 把数据输出-->
<td><?php echo $count++;?></td>
<td><?php echo str_repeat('----',$files['level']).$files['fileName'];?></td>
<!--利用三元运算符 把数组中的数据作为判断条件 如果是文件就输出文件 反之输出文件夹-->
<td><?php echo $files['isDir'] ? '文件夹' : '文件';?></td>
<td>
<!--分别指向处理新增 删除 重命名的php文件 然后php文件在包括其前段文件 -->
<?php echo $files['isDir'] ? "<a href='addFile.php?dir={$files['directory']}&fileName={$files['fileName']}'>新增</a> " : '';?>
<!--点击删除的时候会把要删除的路径和文件名以及初始路径传给delete.php-->
<a href="delete.php?dir=<?php echo $files['directory'];?>&fileName=<?php echo $files['fileName'];?>">删除</a>
<a href="renameFile.php?dir=<?php echo $files['directory'];?>&fileName=<?php echo $files['fileName'];?>">重命名</a>
</td>
</tr>
<?php endforeach;?>
</table>
</body>
</html>
<html>
<!--新增功能的前端页面-->
<head>
<meta charset="utf-8">
</head>
<body>
<h1><?php echo $newDir;?></h1>
<!--把表单中的数据提交到create.php来建立文件以及文件夹-->
<form action="create.php" method="POST">
<!--将需要新增的文件路径传给create.php-->
<input type="hidden" name="dir" value="<?php echo $newDir;?>"/>
<!--hidden是隐藏输入项 不在浏览器中显示出来但是可以把要新增的路径提交给php文件-->
<!--在此的作用是把此文件中的已确定的数据 隐示的传达给create.php文件 -->
<input type="text" name="filename" value=''/>
<!--做一个单选框判断是新增文件还是新增文件夹-->
文件: <input type="radio" name='type' value="a"/>
文件夹: <input type="radio" name='type' value="b"/>
<input type="submit" name='submit' value="go add"/>
</form>
</body>
</html>
<?php
//包含前段文件 并把文件信息通过前端文件传给真正处理新增功能的php文件
//提取到要增加文件的文件夹名称
$dir = $_GET['dir'] or die('文件夹不存在');
$file =$_GET['fileName'] or die('文件名不存在');
//包含add.html
include_once "add.html";
?>
<?php
//此PHP文件的作用是对新增操作进行安全性检验 以及 新增文件和文件夹
//接受文件
$dir = $_POST['dir'] or die('文件路径不存在');
$fileName = $_POST['filename'] or die('没有文件名');
$type =$_POST['type'] or die('未选择文件类型');
//安全检验
function goBack(){
echo '服务器出错';
header('Refresh:3;url=add.html');
exit;
//当路径有问题时 提示错误 3s后返回add.html
}
if(!is_dir($dir)) goBack();
if(empty($fileName)) goBack();
if(!$type=='a'&&!$type=='b') goBack();
//生成文件夹或者文件
if($type=='a'){
$res =@touch($dir.'\\'.$fileName);
}else if($type=='b'){
$res =@mkdir($dir.'\\'.$fileName);
}
//检验结果
if($res){
echo '文件创造成功';
header('Refresh:3;url=getFile.php?dir='.$dir);
exit;
}else{
echo '文件新增失败';
header('Refresh:3;url=first.html');
exit;
}
?>
<?php
//接收需要删除文件或者目录的路径以及名称
$dir=$_GET['dir'] or die('路径不存在');
$fileName =$_GET['fileName'] or die('文件不存在');
$newDir =$dir.'\\'.$fileName;
echo $newDir;
if(is_dir($newDir)){
//删除文件夹
$res = @rmdir($newDir);
echo "删除成功";
exit;
}else{
//删除文件
$res = @unlink($newDir);
echo "删除成功";
exit;
}
?>
<html>
<!--此HTML处理重命名文件的前端 -->
<head>
<meta charset="utf-8">
</head>
<body>
<h1>要修改的文件: <?php echo $renameDir; ?></h1>
<form action='update.php' method="POST">
<input type="hidden" name="dir" value="<?php echo $dir; ?>"/>
<input type="hidden" name="fileName" value="<?php echo $fileName; ?>"/>
<input type="text" name ='newName' value=''/>
<input type="submit" name="submit" value="提交修改"/>
</form>
</body>
</html>
<?php
//接收list.html传来的文件路径与文件名
$dir =$_GET['dir'] or die('无路径');
$fileName=$_GET['fileName']or die('无文件');
$renameDir = $dir.'\\'.$fileName;
//包含前端文件
include_once "rename.html";
?>
<?php
//first.html上传到此php文件 先封装一个当路径出现问题时跳回去的函数
function goBack(){
echo '路径不合法';
header('Refresh:3;url=first.html');
exit;
//当路径有问题时 提示错误 3s后返回first.html
}
//取出传来的路径 调用getFile.php 必须通过getFile传一个路径过来
$dir = $_GET['dir'];
//判断路径是否合法
if(!is_dir($dir)) goBack();
//需要封装一个函数 传入路径 返回数组 数组内容包括 文件名 fileName 文件类型 isDir 文件在第几层 level
include_once "dirFile.php";
//调用getFile函数
$res = getFile($dir);
//对返回数据进行判断
if(!$res) goBack();
//如果数组不存在 退回
//此时虽然已经得到了含有文件信息的数组 但是仍旧无法在浏览器显示 需要包含前端文件
include_once "list.html";
?>
<?php
//此php文件的作用是提供一个可以返回文件信息的数组 是项目的核心内容
/*
* @param1 string $dir 有效路径
* @param2 int level =0 当前文件所在的层级
*/
function getFile($dir,$level=0){
//取出当前路径下的所有文件名
$files=scandir($dir);
//建立一个可以跨函数的数组容器
static $dirFile = array();
//遍历数组
foreach ($files as $value) {
//判断是否是路径
$newDir =$dir.'\\'.$value;
if(is_dir($newDir)){
//把所有的目录信息都放到 $dirFile数组
$dirFile[] = array(
'fileName' => $value,
'directory'=>$dir,
'isDir'=>true,
'level'=>$level
);
//递归之前需要排除每个文件夹都有的 . 和 ..文件夹 否则会进入死循环
if($value =='.'|| $value=='..') continue;
//通过递归 继续把目录下的文件和目录信息放到数组
getFile($newDir,$level +1);
//每次递归 level都+1 代表是下一层目录 函数本身就是递归的出口 当$value不是路径时 就不会调用递归
}else{
//不是路径就代表是文件 同样需要把信息放到$dirFile数组
$dirFile[] = array(
'fileName' => $value,
'directory'=>$dir,
'isDir'=>false,
'level'=>$level
);
}
}
return $dirFile;
}
//测试函数是否有问题
// $res = getFile('F:/server/secondweb');
// foreach ($res as $key => $value) {
// echo $value['directory'].$value['fileName'],'<br/>';
// }
?>
大致效果如下 :
项目比较简单 但是可以把前面学的基础巩固一下 没有用到高端 的前端技术 所以页面只能大致满足操作要求 而不美观。