I can’t solve the problem.
You need to get the directory structure on the server as a list.
There is a php script on the server that the application accesses and in response receives a Json response from the server directories.
Directory structure:
php script for getting a tree of directories and files:
<?php
/*
* Converts a filesystem tree to a PHP array.
*/
function dir_to_array($dir)
{
if (! is_dir($dir)) {
// If the user supplies a wrong path we inform him.
return null;
}
// Our PHP representation of the filesystem
// for the supplied directory and its descendant.
$data = [];
foreach (new DirectoryIterator($dir) as $f) {
if ($f->isDot()) {
// Dot files like '.' and '..' must be skipped.
continue;
}
$path = $f->getPathname();
$name = $f->getFilename();
if ($f->isFile()) {
$data[] = [ 'file' => $name ];
} else {
// Process the content of the directory.
$files = dir_to_array($path);
$data[] = [ 'dir' => $files,
'name' => $name ];
// A directory has a 'name' attribute
// to be able to retrieve its name.
// In case it is not needed, just delete it.
}
}
// Sorts files and directories if they are not on your system.
\usort($data, function($a, $b) {
$aa = isset($a['file']) ? $a['file'] : $a['name'];
$bb = isset($b['file']) ? $b['file'] : $b['name'];
return \strcmp($aa, $bb);
});
return $data;
}
/*
* Converts a filesystem tree to a JSON representation.
*/
function dir_to_json($dir)
{
$data = dir_to_array($dir);
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
return $data;
}
echo dir_to_json('manuals-test/');
?>
The structure of the Json response from the server: (Checked the structure in an online check: valid (RFC 8259))
[
{
"dir":[
{
"dir":[
{
"file":"file1.txt"
},
{
"file":"file2.txt"
}
],
"name":"category 1"
},
{
"dir":[
{
"file":"file 1.txt"
},
{
"file":"file 2.txt"
}
],
"name":"category 2"
}
],
"name":"catalog 1"
},
{
"dir":[
{
"dir":[
{
"file":"file 1.txt"
},
{
"file":"file 2.txt"
},
{
"file":"file 3.txt"
}
],
"name":"category 1"
},
{
"dir":[
{
"file":"file 1.txt"
},
{
"file":"file 2.txt"
},
{
"file":"file 3.txt"
},
{
"file":"file 4.txt"
}
],
"name":"category 2"
}
],
"name":"catalog 2"
}
]
But I can’t convert the Json response to a list in the application.
My Blocs and Screen: