Welcome to WebmasterWorld Guest from 54.166.37.177

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)

Senior Member

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

joined:July 3, 2006
posts: 3123
votes: 0


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 Sept 29, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 29, 2008
posts: 52
votes: 0


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)

Senior Member

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

joined:July 3, 2006
posts: 3123
votes: 0


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)

Senior Member

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

joined:July 3, 2006
posts: 3123
votes: 0


...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)

Administrator

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

joined:July 31, 2003
posts:12541
votes: 1


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>';
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members