How can I construct this file tree based on what files the user is allowed to view?

Posted by robert on Stack Overflow See other posts from Stack Overflow or by robert
Published on 2010-12-22T00:52:03Z Indexed on 2010/12/22 0:54 UTC
Read the original article Hit count: 176

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about permissions