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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?