1

I'm trying to create a one file basic PHP file manager where the first thing I'm trying to build is the browsing directories.

My problem is I'm not able to connect DIR paths separately via anchor tag.

Basically I have created an array of path using getcwd() to use them through chdir() function.

Here's the code snippet:

<?php

function browseFiles()
{
    $current_path = getcwd();
    $path_disect = explode('/', $current_path);
    foreach ($path_disect as $e => $value) {
        echo "<a href='$value'>" . $value . "/";
    }
}

browseFiles();

Output on webpage enter image description here

So what I'm trying is:

Mypath: /var/www/html/

When someone clicks on the /html/ or /www/ or /var/ or ("/" at the first which shows the directory and files listing for the root directory) the user should be able to get directory listing of the directory: "/html/" or /www/ or /var/ or "/"

but what my code is doing it is adding the keyword "/html/" to http://localhost/shell/html/ where the folder "html" doesn't exists.

2
  • Think about your use of explode, what it returns, and what you list as the problem in the last sentence of your post. Commented Sep 9, 2019 at 20:36
  • You can use directory iterator: php.net/manual/tr/class.directoryiterator.php Commented Sep 9, 2019 at 22:00

3 Answers 3

1

Build the full path as you go:

function browseFiles(){

        $current_path = getcwd();
        $path_disect = explode('/', $current_path);

        $cur_a_path = "/";
        foreach ($path_disect as $value){
            $cur_seg = $value."/";
            echo "<a href='".$cur_a_path.$cur_seg."'>".$value."/</a>";
            $cur_a_path .= $cur_seg;
        }

}

browseFiles();
Sign up to request clarification or add additional context in comments.

1 Comment

thank you this worked for me. But the only thing which didn't came according to expected output was the starting "/" should also be under anchor linked which will be linked to the root DIR.
0

I didn't test it, but I think this should work for you.

function browseFiles(){

            $current_path = getcwd();
            $path_disect = explode('/', $current_path);

            foreach ($path_disect as $e => $value){
                $curr_path .= $value."/";
                echo "<a href='$curr_path'>".$curr_path."</a>";
            }

}

browseFiles();

Also if you get bored of just current directory, look at:

function browseFiles($current_path = getcwd()){

        $path_disect = explode('/', $current_path);

        foreach ($path_disect as $e => $value){
            $curr_path .= $value."/";
            echo "<a href='$curr_path'>".$curr_path."</a>";
        }
}

function recursiveListing($listings){
    foreach($listings as $listing){
        browseFiles($listing);
        if(is_dir($listing)){
            recursiveListing($listing);
        }
    }
}

//browseFiles();
$all = scandir("/");
recursiveListing($all);

4 Comments

hi this didn't worked. your code looped the current path 3 times as the number of elements present are 3 also isn't the output i want. Its still adding the directory after localhost/.. ..
You're testing this on a web browser... Just realized that. Does you web server have permissions to read all these files in the first place? Also for the append, the link is always going to start after localhost (maybe /shell?) - whatever your web root is.
I stated as much as I asked there. What is your current environment? Where is this script running?
I'm basically from infosec community and trying to code a webshell. So this is a one pager script. Running this on Ubuntu 18.x.x
0

Kinda reminds me of a breadcrumbs approach. This is untested, off the top of the head here. If it doesn't work let me know.

First thing's first, this PHP app is using URLs from the browser, so you should use this to your advantage by capturing the paths.

If you're using Apache, you can use .htaccess to prevent the server from using any file other than the index.php, including 404s.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
  ErrorDocument 404 /index.php
</IfModule>

Next, we have to fix up the file paths in the anchor tags a bit, and make sure its href URLs correspond to the local directory paths you're looking to access.

Index.php:

<?php

# This is the path we want, if it's empty that's fine as its appended to the cwd.
$url = str_replace("/shells", "", $_SERVER['REQUEST_URI']);
$local_path = getcwd() . $url; # Should resolve to /var/www/html/shells

# Change to the corresponding local directory
chdir($local_path);

# List breadcrumbs

function browseFiles(){
  $path_disect = explode('/', $url);
  $curr_path = '/';

  echo `<a title="/" href="/">[root]</a>&nbsp;&gt;&gt;&nbsp;`;

  for ($x = 1; $x <= count($path_disect); $x++) {
   $curr_path += $path_disect[$x];
   echo `<a title="${path_disect}" href="${curr_path}">`.$path_disect[$x].`</a>&nbsp;`;
  }

/* Then do a scandir() on the $local_path variable to loop through the current working directory */

}

browseFiles();

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.