I created a function a while back as an experiment that works in either loading whichever files are included in $siteFiles as a comma-separated list in the path defined in $sitePath or excludes the files in the list.
The issue is, that while it is indeed loading the files which contain configuration as variables, the variables are not available on the site. Using the same include_once() manually does load them and works well so the issue is not in the included files but rather in the way they are loaded when doing to dynamically. What did I miss here?
// LOAD SECURE CONFIGURATION FILES
// include_once("{$SecurePath}config.php");
// include_once("{$SecurePath}keys.php");`
Calling the function
// LOAD SECURE CONFIGURATION FILES
loadPHPFiles($SecurePath, "config.php, keys.php", 1, 0);
The function itself
// INCLUDES FILES FROM LIST IN SPECIFIED DIRECTORY
// $sitePath = PATH FROM WHICH TO LOAD FILES, MUST HAVE TRAILING SLASH
// $siteFiles = EMPTY, FILE NAME, ARRAY OR COMMA-SEPARATED LIST OF FILES
// $includeFiles = 1 TO INCLUDE THOSE LISTED; 0 TO EXCLUDE THOSE LISTED
// $Sort 0: NONE; 0: ASCENDING; 1: DESCENDING
function loadPHPFiles($sitePath, $siteFiles="", $includeFiles=0, $Sort=0) {
$siteFiles = ($siteFiles !== "" && !is_array($siteFiles)) ? array_map('trim',explode(',',$siteFiles)) : $siteFiles;
$allFiles = scandir($sitePath, $Sort);
$allFiles = array_diff($allFiles, array('.', '..', '.htaccess'));
if (is_array($allFiles)) :
foreach ($allFiles as $fileName) :
// INCLUDE ONLY PHP FILES IN $siteFiles ARRAY
if (pathinfo($fileName, PATHINFO_EXTENSION) !== "php") continue;
// IGNORE ANY index.php FILE THAT MAY EXIST
if ($fileName === "index.php") continue;
// EXCLUDE BACKUP FILES AND PREVIOUS VERSIONS
if (str_contains($fileName, 'BAK')) continue;
if (str_contains($fileName, 'OLD')) continue;
if (is_array($siteFiles) && $includeFiles == 1) :
// INCLUDE FILES IN $siteFiles ARRAY; EXCLUDE ALL OTHERS
if (!in_array($fileName,$siteFiles)) continue;
$file = $sitePath.$fileName;
include_once($file);
elseif (is_array($siteFiles) && $includeFiles == 0) :
// EXCLUDE FILES IN $siteFiles ARRAY; INCLUDE ALL OTHERS
if (in_array($fileName,$siteFiles)) continue;
$file = $sitePath.$fileName;
include_once($file);
else :
// INCLUDE ALL PHP FILES IN PATH (USE WITH CAUTION)
$file = $sitePath.$fileName;
include_once($file);
endif;
// PAUSE BRIEFLY FOR EVERY LOOP
// sleep(2);
endforeach;
endif;
}