如何根据允许用户查看的文件构建此文件树?
我有一个如下所示的文件数组:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以解决问题:)
That should do the trick :)