php 文件管理器

 需求分析:

Web应用中,若添加一个类似Windows文件管理器的功能,会让用户在打开后有一种很熟悉的感觉,不仅使得Web应用对文件的管理更加的直观,而且对大多数用户来说,会更容易操作使用,增强了用户的体验。

接下来,通过PHP中的文件目录技术实现文件管理器的功能。

设计:

1ini_set(‘open_basedir’,__DIR__); 通过‘open_basedir’,__DIR__限制只能访问其所在的目录

2rewinddir

3opendirreaddirclosedir

4fopenfreadfclose

5copyrenameunlink

参考:

PHP文件目录操作

 

<?php
//case 26 文件管理器
//设置配置文件中,只能访问本目录
ini_set('open_basedir',__DIR__);
//路径
$path = isset($_GET['path'])?$_GET['path']:'.';
//文件名
$file = '';
//判断,如果是文件类型
if(is_file($path))
{
    //获得文件名
    $file = basename($path);
    //获得路径
    $path = dirname($path);
}
//判断,不是目录
elseif(!is_dir($path))
{
    die('无效的文件路径参数');
}
 
//获得文件列表
function getFileList($path)
{
    //打开目录,获得句柄
    $handle = opendir($path);
    //空数组
    $list = array('dir'=>array(),'file'=>array());
    //从目录总读取文件
    while(false !==($file_name = readdir($handle)))
    {
        //除去上级目录和本级目录
        if($file_name != '.' && $file_name != '..')
        {
            //文件全路径
            $file_path = "$path/$file_name";
            //文件类型
            $file_type = filetype($file_path);
            //判断,文件类型是文件或者目录
            if(!in_array($file_type,array('file','dir')))
            {
                continue;
            }
            //数组填入值
            $list[$file_type][] = array(
                'file_name'=>$file_name,
                'file_path'=>$file_path,
                'file_size'=>round(filesize($file_path)/1024),
                'file_time'=>date('Y/m/d H:i:s',filemtime($file_path)),
            );
        }
    }
    //释放句柄
    closedir($handle);
    return $list;
}
//处理操作
$action = isset($_GET['a'])?$_GET['a']:'';
//根据操作动作,执行相应程序
switch ($action)
{
    //返回上一级
    case 'prev':
        $path = dirname($path);
        break;
    //复制
    case 'copy':
        if($file)
        {
            if(file_exists("$path/$file.txt"))
            {
                die('文件名冲突,复制失败');
            }
            if(!copy("$path/$file","$path/$file.txt"))
            {
                die('复制文件失败');
            }
        }
        break;
    //删除
    case 'del':
        if($file)
        {
            unlink("$path/$file");
        }
        break;
    //重命名
    case 'rename':
        if(isset($_POST['rename']))
        {
            $target = isset($_POST['target'])?trim($_POST['target']):'';
            if($file && $target)
            {
                if(file_exists("$path/$target"))
                {
                    die('目标文件已存在');
                }
                rename("$path/$file","$path/$target");
                header('Location:?path='.$path);
                die;
            }
 
        }
        break;
 
}
 
$file_list = getFileList($path);
 
?>
 
<html>
<head>
    <meta charset="UTF-8">
    <title>文件管理器</title>
    <style>
	body{ font-size:12px;}
	img{ width:20px;}
	a:link, a:visited { color:#555; text-decoration:none; } 
    a:hover, a:active { color:#ff0000; text-decoration:underline;} 
	.tbl{ border-collapse:collapse; width:98%; margin-top:10px; font-size:12px;}
	.tbl th{ text-align:left; background:#ccc;}
	.tbl th,td{ border:1px solid #eee;}
	span{color:#ff0000;}
  </style>
</head>
<body>
<div>
    <a href="?path=<?= $path;?>&a=prev">返回上一级目录</a>
</div>
<?php  if($action == 'rename'): ?>
<form method="post">
    将<span><?= $file;?></span>
    重命名为:<input type="text" value="<?= $file;?>" name="target">
    <input type="submit" name="rename" value="确定">
</form>
<?php endif;?>
 
 
<table width="60%" style="font-size: 9px;text-align: center;">
    <tr>
        <th>图标</th>
        <th>名称</th>
        <th>修改日期</th>
        <th>大小</th>
        <th>路径</th>
        <th>操作</th>
    </tr>
    <?php  foreach ($file_list['dir'] as $v): ?>
    <tr>
        <td><img src="./img/list.png"></td>
        <td><?= $v['file_name'];?></td>
        <td><?= $v['file_time']?></td>
        <td>-</td>
        <td><?= $v['file_path'];?></td>
        <td><a href="?path=<?= $v['file_path'];?>">打开</a></td>
    </tr>
    <?php endforeach;?>
    <?php foreach ($file_list['file'] as $v):?>
    <tr>
        <td><img src="./img/file.png"></td>
        <td><?= $v['file_name'];?></td>
        <td><?= $v['file_time'];?></td>
        <td><?= $v['file_size'];?>KB</td>
        <td><?= $v['file_path'];?></td>
        <td>
            <a href="?path=<?= $v['file_path']?>&a=rename">重命名</a>&nbsp;&nbsp;
            <a href="?path=<?= $v['file_path']?>&a=copy">复制</a>&nbsp;&nbsp;
            <a href="?path=<?= $v['file_path']?>&a=del">删除</a></td>
    </tr>
    <?php endforeach;?>
</table>
</body>
</html>

 

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北街风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值