I want to determine if the PHP file I am on install.php is in a subdomain/subdirectory (basically not at domain.com/install.php).
Found solution to my problems
see below
I want to determine if the PHP file I am on install.php is in a subdomain/subdirectory (basically not at domain.com/install.php).
see below
if ($handle = opendir('directory/subdirectory/')) {
while (false !== ($entry = readdir($handle))) {
if (strpos($entry, '.php') !== false) {
echo $entry."<br>";
}
}
closedir($handle);
}
My answer is not 100% complete because it hasn't been tested on all server environments, but I can confirm this will work if using the embedded PHP web engin or using an apache server with a symlinked web folder. The function returns the subfolder in the form /folder because that is what I needed. If someone can check Nginx it will help a lot. Notice the use of CONTEXT_DOCUMENT_ROOT vs DOCUMENT_ROOT && SCRIPT_NAME vs REQUEST_URI because that could mean a router URL vs actual live file. To use as per the asked question you could say
If (!empty(getSubFolder())) {
//Do some stuff here
}
/**
* Logic to determine the subfolder - result must be /folder
*/
function getSubFolder() {
//Evaluate DOCUMENT_ROOT && CONTEXT_DOCUMENT_ROOT
$documentRoot = "";
if (isset($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
$documentRoot = $_SERVER["CONTEXT_DOCUMENT_ROOT"];
} else
if (isset($_SERVER["DOCUMENT_ROOT"])) {
$documentRoot = $_SERVER["DOCUMENT_ROOT"];
}
$scriptName = $_SERVER["SCRIPT_FILENAME"];
//echo str_replace($documentRoot, "", $scriptName);
$subFolder = dirname( str_replace($documentRoot, "", $scriptName));
if ($subFolder === "/" || (str_replace($documentRoot, "", $scriptName) === $_SERVER["SCRIPT_NAME"] && $_SERVER["SCRIPT_NAME"] === $_SERVER["REQUEST_URI"])) {
$subFolder = null;
}
return $subFolder;
}