note 28292 added to function.dirname

From: Date: Wed, 08 Jan 2003 16:20:29 +0000
Subject: note 28292 added to function.dirname
Groups: php.notes 
Request: Send a blank email to php-notes+get-41835@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: 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://www.php.net/manual/en/function.dirname.php http://master.php.net/manage/user-notes.php?action=edit+28292 http://master.php.net/manage/user-notes.php?action=delete+28292 http://master.php.net/manage/user-notes.php?action=reject+28292

« previous php.notes (#41835) next »