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?