note 28292 modified in function.dirname by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 19:12:18 +0000 |
| Subject: | note 28292 modified in function.dirname by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142282@lists.php.net to get a copy of this message | ||
I very much appreciated Fredrich Echol's suggestion (rwf at gpcom dot net) of how to find a
base path, but found that it failed when the initial script was already in the root folder --
dirname('/rootscript.php')=='/' and
dirname('/include/includescript.php')=='/include' which have the same number of
slashes. This variation is what I'm now using:
<?php
if (!defined("BASE_PATH")) define('BASE_PATH',
dirname($_SERVER['SCRIPT_NAME'])=='/' ? './' :
str_repeat("../", substr_count(dirname($_SERVER["SCRIPT_NAME"]),
"/")));
?>
This explicitly checks for the root path (/) and uses './' as the base path if we're
in the root folder.
I put this at/near the top of any file that calls another. (I used define for my own convenience;
should work just fine with variables and without testing to see if you already did it.)
Note that in both cases (root-folder script and non-root-folder script), BASE_PATH will include a
trailing slash. At least with Apache on Darwin (Mac OS X), you can
include(BASE_PATH.'/myfile.php'); and the doubled slash won't cause any problems,
giving the same result as include(BASE_PATH.'myfile.php'); .
--was--
I very much appreciated Fredrich Echol's suggestion (rwf at gpcom dot net) of how to find a
base path, but found that it failed when the initial script was already in the root folder --
dirname('/rootscript.php')=='/' and
dirname('/include/includescript.php')=='/include' which have the same number of
slashes. This variation is what I'm now using:
if (!defined("BASE_PATH")) define('BASE_PATH',
dirname($_SERVER['SCRIPT_NAME'])=='/' ? './' :
str_repeat("../", substr_count(dirname($_SERVER["SCRIPT_NAME"]),
"/")));
This explicitly checks for the root path (/) and uses './' as the base path if we're
in the root folder.
I put this at/near the top of any file that calls another. (I used define for my own convenience;
should work just fine with variables and without testing to see if you already did it.)
Note that in both cases (root-folder script and non-root-folder script), BASE_PATH will include a
trailing slash. At least with Apache on Darwin (Mac OS X), you can
include(BASE_PATH.'/myfile.php'); and the doubled slash won't cause any problems,
giving the same result as include(BASE_PATH.'myfile.php'); .
http://php.net/manual/en/function.dirname.php