Php 如何根据允许用户查看的文件构造此文件树?
我有一组文件,如下所示:Php 如何根据允许用户查看的文件构造此文件树?,php,permissions,tree,directory,Php,Permissions,Tree,Directory,我有一组文件,如下所示: Array ( [0] => Array ( [type] => folder [path] => RootFolder ) [1] => Array ( [type] => file [path] => RootFolder\error.log )
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
)
)
我解析这个数组,并根据文件的深度(“/”计数)创建一个树状视图。看起来是这样的:
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
)
)
我现在拥有的是一组允许用户查看的文件路径。在构建上面的树时,我需要考虑这个数组。该数组如下所示:
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
)
)
如果在数组($path,$allowed)中执行将很容易
,但这不会给出树。只是一个文件列表
另一个让我费解的部分是这个要求:如果用户有权查看文件夹test
,那么他们就有权访问该文件夹的所有子文件夹
我的想法是简单地解析文件路径。例如,我要确认RootFolder\test\sd
是一个目录,然后根据“/”计数创建一个树。就像我之前做的那样。然后,由于这是一个目录,我会取出这个目录中的所有文件,并将它们显示给用户。但是,我无法将其转换为工作代码
有什么想法吗
$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');
这应该可以做到:)