【问题标题】:Having directory path in an array and browsing them PHP在数组中有目录路径并浏览它们 PHP
【发布时间】:2019-09-09 20:34:12
【问题描述】:

我正在尝试创建一个单一文件的基本 PHP 文件管理器,其中我尝试构建的第一件事是浏览目录。

我的问题是我无法通过锚标记单独连接 DIR 路径。

基本上我已经使用 getcwd() 创建了一个路径数组,以便通过 chdir() 函数使用它们。

这里是sn-p的代码:

<?php

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

browseFiles();

网页输出

所以我正在尝试的是:

我的路径:/var/www/html/

当有人点击 /html/ 或 /www/ 或 /var/ 或(“/”在第一个显示根目录的目录和文件列表)时,用户应该能够获得目录列表目录:“/html/”或/www/或/var/或“/”

但我的代码正在做的是将关键字“/html/”添加到文件夹“html”不存在的http://localhost/shell/html/

【问题讨论】:

标签: javascript php html


【解决方案1】:

边走边构建完整路径:

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();

【讨论】:

  • 谢谢你这对我有用。但唯一没有达到预期输出的是起始“/”也应该在锚链接下,该锚链接将链接到根目录。
【解决方案2】:

我没有测试它,但我认为这应该适合你。

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();

此外,如果您对当前目录感到厌烦,请查看:

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);

【讨论】:

  • 嗨,这没有用。您的代码循环当前路径 3 次,因为存在的元素数量为 3 也不是我想要的输出。它仍在 localhost/.. .. 之后添加目录
  • 您正在 Web 浏览器上进行测试...刚刚意识到这一点。您的网络服务器是否有权首先读取所有这些文件?同样对于附加,链接总是在 localhost 之后开始(可能是 /shell?) - 无论您的 Web 根目录是什么。
  • 我说的和我在那里问的一样多。你现在的环境是什么?这个脚本在哪里运行?
  • 我基本上来自 infosec 社区并尝试编写 webshel​​l。所以这是一个单页脚本。在 Ubuntu 18.x.x 上运行它
【解决方案3】:

有点让我想起了面包屑的方法。这是未经测试的,这里是头顶。如果它不起作用,请告诉我。

首先,这个 PHP 应用程序使用来自浏览器的 URL,因此您应该通过捕获路径来利用这一点。

如果您使用的是 Apache,则可以使用 .htaccess 来防止服务器使用除 index.php 以外的任何文件,包括 404。

<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>

接下来,我们必须稍微修正一下锚标记中的文件路径,并确保其 href URL 对应于您要访问的本地目录路径。

索引.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();

?>

【讨论】:

    猜你喜欢
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多