Forum Moderators: coopster
Example...
1. Apache/Linux, test_path.php is in a subfolder - ALL GOOD
$_SERVER["SCRIPT_NAME"] = /test/test_path.php
dirname($_SERVER["SCRIPT_NAME"]) = /test
2. Apache/Windows, test_path.php is in a subfolder - ALL GOOD (Same as above)
$_SERVER["SCRIPT_NAME"] = /test/test_path.php
dirname($_SERVER["SCRIPT_NAME"]) = /test
3. Apache/Linux, test_path.php is in the web root - ALL GOOD
$_SERVER["SCRIPT_NAME"] = /test_path.php
dirname($_SERVER["SCRIPT_NAME"]) = /
4. Apache/Windows, test_path.php is in the web root - WHY THE BACKSLASH?!
$_SERVER["SCRIPT_NAME"] = /test_path.php
dirname($_SERVER["SCRIPT_NAME"]) = \
I guess in 4. PHP is returning the value of DIRECTORY_SEPARATOR (since dirname() would ordinarily return an empty string in this instance? I'm guessing). But why doesn't it return a forward slash as per the path being examined?!
To put it another way... if dirname() changes the forward slash to a backslash when the path consists of just the root (on Windows), why does it not do this for other paths?
$dir = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/.\\');
$dirs = array(
'/dir/file',
'/dir/dir/',
'/file',
'/dir/',
'file'
);
print '<pre>';
print_r($dirs);
foreach ($dirs as $dir) {
print dirname($dir) . "\n";
}
print '</pre>';