In reference to this posting I could use a little help with RecursiveDirectoryIterator. The function as posted originally in the other thread is now obsolete so below is the new one which is working except for the first item. It resists my programming to change the key or format the path itself as it’s apparently coming from somewhere other than the foreach loop. This is the first time I’ve used RecursiveDirectoryIterator and know little about it so please advise!
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CATCH_GET_CHILD,
RecursiveIteratorIterator::SELF_FIRST
);
It’s giving a path like this:
array
(
[0] => /var/www/html/common
[CommonRelMessages] => /common/messages/
[CommonRelImages] => /common/images/
[CommonRelIcaptcha] => /common/icaptcha/
[CommonRelScripts] => /common/scripts/
[CommonRelTruetype] => /common/truetype/
)
. . . but I need this that my programming is supposed to provide:
array
(
[CommonRel] => /common/
[CommonRelMessages] => /common/messages/
[CommonRelImages] => /common/images/
[CommonRelIcaptcha] => /common/icaptcha/
[CommonRelScripts] => /common/scripts/
[CommonRelTruetype] => /common/truetype/
)
For reference, here is the entire function:
function listFolders(& $systemFolder, $pathtype='Absolute', $removefolder=0, $relative=0, $trailing=1, $common=0) {
$folderArray = (is_array($systemFolder)) ? [$systemFolder] : explode(',', $systemFolder);
$folderArray = array_map('trim', $folderArray);
// CONSTRUCT ABSOLUTE PATH FROM FOLDER(S) SPECIFIED
$folder = buildPath($folderArray, $removefolder, 0, 0, $common);
// DEFINE TRAILING SLASH IF SPECIFIED
$trailing = ($trailing > 0) ? DIRECTORY_SEPARATOR : "";
// BUILD THE RECURSIVE ITERATOR OBJECT
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CATCH_GET_CHILD,
RecursiveIteratorIterator::SELF_FIRST
);
// INITIALIZE $paths ARRAY
$paths = [$folder];
foreach ($iterator as $path => $dir) :
if ($dir->isDir()) :
// BUILD NEW KEY NAMES
$key_1 = ucfirst(basename(dirname($path)));
$key_2 = (basename($dir)) ? ucfirst(basename($dir)) : "";
$key = $key_1 . $pathtype . $key_2;
// IGNORE CERTAIN FOLDERS
if (str_contains($key, 'Verified')) continue;
if (str_contains($key, 'Notes')) continue;
if (str_contains($key, 'OBSOLETE')) continue;
if (str_contains($key, 'OLD')) continue;
if (str_contains($key, 'BAK')) continue;
// REWRITE $path BASED ON PASSED IN PARAMETERS
if (basename(dirname($dir)) === $systemFolder) :
$buildPath = [$systemFolder, basename($dir)];
else :
$buildPath = basename($dir);
endif;
$path = buildPath($buildPath, $removefolder, $relative, $trailing, $common);
$paths[$key] = $path . $trailing;
endif;
endforeach;
return $paths;
}