如何根据允许用户查看的文件构建此文件树?

发布于 2024-10-08 17:31:33 字数 1507 浏览 0 评论 0原文

我有一个如下所示的文件数组:

Array
(
    [0] => Array
        (
            [type] => folder
            [path] => RootFolder
        )

    [1] => Array
        (
            [type] => file
            [path] => RootFolder\error.log
        )

    [2] => Array
        (
            [type] => folder
            [path] => RootFolder\test
        )

    [3] => Array
        (
            [type] => file
            [path] => RootFolder\test\asd.txt
        )

    [4] => Array
        (
            [type] => folder
            [path] => RootFolder\test\sd
        )

    [5] => Array
        (
            [type] => file
            [path] => RootFolder\test\sd\testing.txt
        )
)

我解析该数组并根据文件的深度(“/”计数)创建一个类似树的视图。它看起来像这样:

RootFolder
    - error.log
    - test
        - asd.txt
        - sd
            - testing.txt

我现在拥有的是允许用户查看的文件路径数组。在构建上面的树时我需要考虑这个数组。该数组看起来像这样:

Array
(
    [0] => Array
        (
            [filePath] => RootFolder\test\sd
        )

    [1] => Array
        (
            [filePath] => RootFolder\error.log
        )

)

执行 if in_array($path, $allowed) 很容易,但这不会给我树。只是文件列表...

我困惑的另一部分是这个要求:如果用户有权查看文件夹 test,那么他们就可以访问该文件夹的所有子文件夹。

我的想法是简单地解析文件路径。例如,我会确认 RootFolder\test\sd 是一个目录,然后根据“/”计数创建一棵树。就像我之前做的那样。然后,由于这是一个目录,我将提取该目录中的所有文件并将它们显示给用户。但是,我无法将其转换为工作代码......

有什么想法吗?

I have an array of files that looks like this:

Array
(
    [0] => Array
        (
            [type] => folder
            [path] => RootFolder
        )

    [1] => Array
        (
            [type] => file
            [path] => RootFolder\error.log
        )

    [2] => Array
        (
            [type] => folder
            [path] => RootFolder\test
        )

    [3] => Array
        (
            [type] => file
            [path] => RootFolder\test\asd.txt
        )

    [4] => Array
        (
            [type] => folder
            [path] => RootFolder\test\sd
        )

    [5] => Array
        (
            [type] => file
            [path] => RootFolder\test\sd\testing.txt
        )
)

I parse this array and create a tree like view based on the depth of the files ('/' count). It looks like this:

RootFolder
    - error.log
    - test
        - asd.txt
        - sd
            - testing.txt

What I have now is an array of filepaths the user is allowed to view. I need to take this array into account when constructing the above tree. That array looks like this:

Array
(
    [0] => Array
        (
            [filePath] => RootFolder\test\sd
        )

    [1] => Array
        (
            [filePath] => RootFolder\error.log
        )

)

It would be easy to do a if in_array($path, $allowed) but that won't give me the tree. Just a list of files...

Another part I'm stumped on is this requirement: If the user has access to view the folder test, they then have access to all children of that folder.

My idea was to simply parse the filepaths. For example, I'd confirm that RootFolder\test\sd was a directory and then create a tree based on the '/' count. Like I was doing earlier. Then, since this is a directory, I'd pull out all files within this directory and show them to the user. However, I'm having trouble converting this to working code...

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你对谁都笑 2024-10-15 17:31:33
$tree = array();
// keep one:
$permNeeded = '?'; //something you're searching for exactly
$permNeeded = array('?', '?'); // multiple allowed perms
// be carefull with octal data checking permisions!

function checkPerms($permFileHas){
    // keep one:
    return $permFileHas==$permNeeded;
    return in_array($permFileHas, $permNeeded);
}

function parseDir($dir){
    $contents = scandir($dir);
    foreach($contents as $file){
        if(in_array($file, array('.', '..')){
            continue; // skip . and ..
        }
        if(is_dir($file)){
            parseDir($file);
            continue;
        }
        if(checkPerms(fileperms($file)){
            $tree[] = $dir.DIRECTORY_SEPARATOR.$file;
        }
    }
}

parseDir('/the/dir/user/have/perms');

这应该可以解决问题:)

$tree = array();
// keep one:
$permNeeded = '?'; //something you're searching for exactly
$permNeeded = array('?', '?'); // multiple allowed perms
// be carefull with octal data checking permisions!

function checkPerms($permFileHas){
    // keep one:
    return $permFileHas==$permNeeded;
    return in_array($permFileHas, $permNeeded);
}

function parseDir($dir){
    $contents = scandir($dir);
    foreach($contents as $file){
        if(in_array($file, array('.', '..')){
            continue; // skip . and ..
        }
        if(is_dir($file)){
            parseDir($file);
            continue;
        }
        if(checkPerms(fileperms($file)){
            $tree[] = $dir.DIRECTORY_SEPARATOR.$file;
        }
    }
}

parseDir('/the/dir/user/have/perms');

That should do the trick :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文