Welcome to WebmasterWorld Guest from 54.197.94.141

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

dirname() root folder on Apache/Windows becomes backslash

Can anyone explain why?

   
10:14 am on Sep 26, 2008 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



I realise this behaviour is according to the manual [uk.php.net], but can anyone explain to me why?

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?!

3:45 am on Sep 29, 2008 (gmt 0)

5+ Year Member



Open up your 'My Computer', Double click on your C: drive, browse to any random folder, say 'Program Files', now look at the address bar in the window ... Windows just prefers to use backslashes instead when most other OSes, if not all other operating systems use a forward slash. Same difference.
9:30 am on Oct 16, 2008 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Yes I realise Windows uses the backslash as the directory separator, but my point is that Apache (running on Windows) is returning the forward slash in all paths ($_SERVER["SCRIPT_NAME"] etc.) and dirname() (which merely manipulates the path string - it does not check for valid paths etc.) is returning the expected forward-slashed 'string' in all cases, except when the path consists of just the root directory when the forward slash is changed to a backslash (the value of DIRECTORY_SEPARATOR).

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?

1:09 pm on Oct 16, 2008 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



...when the path consists of just the root directory then the forward slash is changed to a backslash (the value of DIRECTORY_SEPARATOR).

Just to add, it would still be a perfectly valid path (even when running on Windows) if it was kept as a forward slash.

9:01 pm on Oct 20, 2008 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I've never pulled the source code to check it out, but I realized this same fact long ago myself. I was using dirname to trim and get the directory structure for manipulation/redirection and ended up with unexpected results when the path handed to the function was in the root. I found myself using rtrim() to touch things up ...
$dir = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/.\\');

Then I would append my path/files to the $dir variable as necessary. Why include the period in there? Current directory. Try this to see what I mean:
$dirs = array( 
'/dir/file',
'/dir/dir/',
'/file',
'/dir/',
'file'
);
print '<pre>';
print_r($dirs);
foreach ($dirs as $dir) {
print dirname($dir) . "\n";
}
print '</pre>';
 

Featured Threads

Hot Threads This Week

Hot Threads This Month