文件的复制删除重命名

修改filemanager_html.php代码:

<?php
<!--    循环输出文件列表-->
    <?php
    foreach($file_list['file'] as $v):
    ?>
    <tr>
        <td><img src="./img/file.png"><?php echo $v['filename'];?></td>
        <td><?php echo $v['filemtime'];?></td>
        <td><?php echo $v['filesize'];?>KB</td>
        <td>
            <a href="?path=<?php echo $v['filepath'];?>&a=rename">重命名</a>
            <a href="?path=<?php echo $v['filepath'];?>&a=copy">复制</a>
            <a href="?path=<?php echo $v['filepath'];?>&a=del">删除</a>
        </td>
    </tr>
    <?php endforeach;?>

修改filemanager.php代码:

<?php
//获取操作参数
$action=isset($_GET['a'])?$_GET['a']:'';
switch ($action){
    //返回上一级目录
    case 'prev':
        $path=dirname($path);
        break;
    //其他操作...
    case 'copy':
        if($file){
            if(file_exists("$path/$file.bak")){
                die('文件名冲突,复制失败');
            }
            if(!copy("$path/$file","$path/$file.bak")){
                die('复制文件失败');
            }
        }
        break;
    case 'del':
        if($file){
            unlink("$path/$file");
        }
        break;
    case 'rename':
        if(!empty($_POST)){
            //获取目标文件名
            $target=isset($_POST['target'])?trim($_POST['target']):'';
            //如果待操作文件不为空,则进行重命名操作
            if($file && $target){
                if(file_exists("$path/$target")){
                    die('目标文件已经存在');
                }
                rename("$path/$file","$path/$target");
            }
            //重命名完成后跳转
            header('Localtion:?path='.$path);
        }
        break;
}

重命名需要在‘返回上一级目录’上面加上一段代码用于重新命名新的文件名称:

<?php
<!--重命名操作区-->
<?php  if($action=='rename'):?>
<form method="post">
    将<span><?php echo $file;?></span>
    重命名为:<input type="text" value="<?php echo $file;?>" name="target" />
    <input type="submit" value="确定"/>
</form>
<?php endif;?>

复制效果展示:


微信图片_20180301170355.png

删除展示:

微信图片_20180301170533.png微信图片_20180301170536.png

重命名展示:

微信图片_20180301170738.png微信图片_20180301170741.png微信图片_20180301170744.png

继续学习
||
<?php echo "复制,删除,重命名操作";
提交重置代码
章节
笔记
提问
课件
反馈
捐赠