dotaer1993 2013-07-18 23:42 采纳率: 100%
浏览 63
已采纳

使用array_slice()转到上一个文件夹

I have build my very first file manager and I need some help with the navigation section. Here's the code for this section:

# CONFIGURATION: Folder
$path = (empty($_GET['p']) ? '../../../' : '../../../'.$_GET['p']);

# CONTROL: The folder exists
if(file_exists($path)) {
    $results = scandir($path);
}



# CONTROL: Root
if(!empty($_GET['p'])) {
    $navigation_loop = explode('/', $_GET['p']);

    if(count($navigation_loop) > 1) {
        $sliced = array_slice($navigation_loop, 0, -1);
    }


    # LOOP
    foreach($navigation_loop AS $navigation) {
        echo '<a href="javascript:void(0)" class="filemanager-link" id="path-navigation" data="';

        # CONTROL: There's more than one
        if(count($navigation_loop) > 1) {
            echo implode('/', $sliced);

        # CONTROL: There's not more than one
        } else {
            echo $navigation;
        }

        echo '">';
            echo $navigation;
        echo '</a>';
    }
}

$_GET['p'] contains the full path to the current folder, i.e. some/path/to/show/you. The file name are never shown in this GET!

Now here's the problem: when I'm at some/path and clicking on some, the website takes me to the folder some. But if I'm at some/path/to and clicking on some, the website just takes me to some/path.

I know what the problem is (array_slice($navigation_loop, 0, -1)) but I don't know how I can fix this problem. If I'm at some/path it will be -1 for the array_slice() function. But when I'm at some/path/to it should be -2 if I want to go to some and -1 if I want to go to some/path.

How can I fix this issue?

  • 写回答

2条回答 默认 最新

  • duanhui4160 2013-07-19 00:21
    关注

    Try this for your main if statement:

    if(!empty($_GET['p'])) {
        $navigation_loop = explode('/', $_GET['p']);
    
        # LOOP
        for ($level=0; $level < count($navigation_loop); $level++) {
            echo '<a href="javascript:void(0)" class="filemanager-link" id="path-navigation" data="';
    
            # CONTROL: There's more than one
            $sliced = array_slice($navigation_loop, 0, ($level+1));
            if(count($navigation_loop) > 1) {
                echo implode('/', $sliced);
            # CONTROL: There's not more than one
            } else {
                echo $sliced;
            }
    
            echo '">';
                echo $navigation_loop[$level];
            echo '</a>';
        }
    }
    

    Should be more like what you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器