Answer the question
In order to leave comments, you need to log in
How to loop through a directory and add files to an array?
For example, there is the following directory structure
cat
dir1
subdir1
/* иные папки или файлы*/
subdir2
/* иные папки или файлы*/
dir2
subdir3
/* иные папки или файлы*/
subdir4
/* иные папки или файлы*/
Answer the question
In order to leave comments, you need to log in
fucntion getFileNames($root)
{
$out = array();
$files = scandir($root);
for ($i = 0; $i < count($files); $i++)
{
$name = $files[$i];
if ($name == "." || $name == "..")
{
continue;
}
if (is_dir($root . "/" . $name))
{
$out = array_merge($out, getFileNames($root . "/" . $name));
}
else
{
$out[] = $name;
}
}
return $out;
}
function convertArray($array, $path)
{
$result = array();
for ($i = 0; $i < count($array); $i++)
{
$result[] = $path . "/" . $array[$i];
}
return $result;
}
fucntion getFileNames($root)
{
$out = array();
$files = convertArray(scandir($root), $root);
while (count($files))
{
$path = array_shift($files);
if (basename($path) == "." || basename($path) == "..")
{
continue;
}
if (is_dir($path))
{
$files = array_merge($files, convertArray(scandir($path), $path);)
}
else
{
$out[] = basename($name);
}
}
return $out;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question