RecursiveDirectoryIterator and RecursiveIteratorIterator

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;
}

I found the bug, a simple tweak to remove $folder from the array initialization from $paths = [$folder]; to simply $paths = [];. It was a no-brainer but I was so busy focusing on the unfamiliar RecursiveDirectoryIterator object that I missed the obvious. Odd no one here spotted and commented on it.

That said, it is still not working as expected. Given that I am feeding it a full path of /var/www/html/test.loc/internals/, it is giving only a single layer of folders under it and the $path array is missing them so they are disappearing, or not being fetched in the first place, before any filtering. If any of those folders have their own sub-folders, they are not showing.

I see it is also not showing the path itself - the one I gave it to begin with. I need not only recursive but also inclusive. Is there such a thing?

How can this be corrected? Help appreciated.

Sponsor our Newsletter | Privacy Policy | Terms of Service