我现在有点迷失在这里.我的目标是递归扫描每个子文件夹中包含子文件夹和图像的文件夹,将其转换为多维数组,然后能够使用其包含的图像解析每个子文件夹.
我有以下开始代码,它基本上扫描包含文件的每个子文件夹,然后丢失以使其现在进入多数组.
$dir = 'data/uploads/farbmuster';
$results = array();
if(is_dir($dir)) {
$iterator = new RecursiveDirectoryIterator($dir);
foreach(new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
if($file->isFile()) {
$thispath = str_replace('\\','/',$file->getPath());
$thisfile = utf8_encode($file->getFilename());
$results[] = 'path: ' . $thispath. ', filename: ' . $thisfile;
}
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我弄这个吗?
提前致谢!
我正在开发一个管理面板,显示服务器上特定位置的目录结构.我有一个递归的PHP函数,遍历每个文件和文件夹.我无法弄清楚的是如何将这个目录结构存储在php关联数组中,如下所示:
array[foldername1][0]=file; // if the foldername1 contains a file
array[foldername1][foldername2][0]=file //if foldername1 contains another folder(foldername2) along with the file.
Run Code Online (Sandbox Code Playgroud)
我想遵循的规则是; 一个文件夹应始终是一个键,文件应始终在这样的索引:
array[folder1][folder2][0]=file1;
array[folder1][folder2][1]=file2;
Run Code Online (Sandbox Code Playgroud)
填充此关联数组的函数应该是通用的,因为我们永远不知道目录结构是什么.我想json_encode这个数组回到我的客户端,并在javascript中处理它,这不是一个问题.
如果这是一个不好的方法,请让我知道,可能有更好的方法来做到这一点.我想过使用扁平阵列,但我认为这是一个糟糕的设计.