This basic function called listFolders() is working well enough and builds upon an existing function that I’ve been using for quote a while called buildPath(). BuildPath() which I included for reference is simply fed a root folder name or a comma-separated list of the root folder and subfolder(s) and it builds a full or relative path to it depending on the parameters being passed. All this is working on my test site until I realized that the test site has no subfolders under any of the others so as a test I created one and found that it doesn’t show. The listFolder() function calls the folders inside the one being passed into it but no subfolders within those and I’m not sure how to proceed. Any ideas?
<?php
// BUILD PATH FROM INPUT VALUES
// $systemFolder IS A FOLDER OR COMMA-SEPARATED LIST CONTAINING THE FOLDER(S) TO USE
// $removefolder WHEN TRUE (1) REMOVES THE SITE'S ROOT FOLDER
// $relative WHEN TRUE (1) SHOWS THE PATH TO THE SITE'S ROOT FOLDER
// $trailing IF TRUE (1) ADDS A TRAILING SLASH TO THE PATH
// $common SPECIFIES IF FOLDER IS OUTSIDE THE SITE FILE SYSTEM
function listFolders($systemFolder, $valueType="", $removefolder=0, $relative=0, $trailing=1, $common=0) {
$folderArray = (is_array($systemFolder)) ? [$systemFolder] : explode(',', $systemFolder);
// CONSTRUCT ABSOLUTE PATH FROM FOLDER(S) SPECIFIED
$systemPath = buildPath($folderArray, $removefolder, 0, 1, $common);
// ADD TRAILING SLASH IF SECIFIED
$trailing = ($trailing > 0) ? DIRECTORY_SEPARATOR : "";
// INITIALIZE ARRAY TO STORE FOLDER NAMES
$folders = [];
// SCAN THE CONSTRUCTED DIRECTORY
$items = scandir($systemPath);
if (is_array($items)) :
if (is_dir($systemPath)) :
foreach ($items as $item) :
// IGNORE DOT, OLD, OBSOLETE AND BAK FOLDERS
if (str_contains($item, '.')) continue;
if (str_contains($item, '..')) continue;
if (str_contains($item, 'OBSOLETE')) continue;
if (str_contains($item, 'OLD')) continue;
if (str_contains($item, 'BAK')) continue;
// GENERATE PATH BASED ON VALUES PASSED
$path = buildPath($folderArray, $removefolder, $relative, $trailing, $common);
// BUILD VARIABLE NAME
$varname = ucfirst($item) . $valueType;
// REBUILD THE PATH FOR OUTPUT
$path = $path . DIRECTORY_SEPARATOR . $item . $trailing;
// BUILD ARRAY WITH FOLDER NAME AS KEY AND FULL PATH AS VALUE
$folders[$varname] = $path;
endforeach;
return $folders;
endif;
endif;
}
function buildPath($folders, $removefolder=0, $relative=1, $trailing=1, $common=0) {
$folders = (!is_array($folders)) ? [$folders] : $folders;
if (is_array($folders)) :
// DEFINE VARIABLES
$directoryseparator = DIRECTORY_SEPARATOR;
$trailing = ($trailing > 0) ? $directoryseparator : "";
$path = $_SERVER['DOCUMENT_ROOT'] . $directoryseparator . join($directoryseparator, $folders) . $trailing;
$sitepath = $_SERVER['DOCUMENT_ROOT']; // /var/www/html/test.loc
// DEFINE SITE DOMAIN NAME
$sitefolder = $_SERVER['SERVER_NAME']; // text.loc
// CREATE PATH BASED ON VALUES PASSED TO FUNCTION
$path = ($common > 0) ? str_replace("$sitefolder/","",$path): $path;
$path = ($removefolder > 0 && $relative > 0) ? str_replace("$sitepath","",$path) : $path;
$path = ($removefolder > 0) ? str_replace("$sitefolder/","",$path): $path;
$path = ($relative > 0) ? str_replace($sitepath,"",$path): $path;
return $path;
endif;
}
// CALLED USING
listFolders("internals", $valueType="", 0, 0, 1, 0); // LIST internals FOLDERS
// EXAMPLE OF OUTPUT
/*
Array
(
[Configuration] => /var/www/html/test.loc/internals/configuration/
[Functions] => /var/www/html/test.loc/internals/functions/
[Includes] => /var/www/html/test.loc/internals/includes/
[Styles] => /var/www/html/test.loc/internals/styles/
)
*/
?>