使用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?

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.

Can't you calculate the offset?

$navigation_loop = explode('/', $_GET['p']);
$offset = -1 * (count($navigation_loop) - 1);
if(count($navigation_loop) > 1) {
    $sliced = array_slice($navigation_loop, 0, $offset);
}

I'm not sure I fully understand the problem, but this seems like it should get the right parameters for the slice call.