| dirname() root folder on Apache/Windows becomes backslash Can anyone explain why? |
penders

msg:3752502 | 10:14 am on Sep 26, 2008 (gmt 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?!
|
grallis

msg:3754132 | 3:45 am on Sep 29, 2008 (gmt 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.
|
penders

msg:3766916 | 9:30 am on Oct 16, 2008 (gmt 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?
|
penders

msg:3767019 | 1:09 pm on Oct 16, 2008 (gmt 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.
|
coopster

msg:3769932 | 9:01 pm on Oct 20, 2008 (gmt 0) | 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>';
|
|
|