OM2 Posted February 5, 2009 Share Posted February 5, 2009 Can someone tell me how I get JUST the current directory of the calling script? I've looked up and there a few options: but I end with the full directory location All I want is *just* the directory name I would have thought that there would be a constant? I've come across the following script a few times while googling: function getCurrentDirectory() { $path = dirname($_SERVER['PHP_SELF']); $position = strrpos($path,'/') + 1; return substr($path,$position); } Is this the only or best way to get the current directory name? Thanks OM Link to comment https://forums.phpfreaks.com/topic/143978-how-do-i-get-the-current-directory/ Share on other sites More sharing options...
Mark Baker Posted February 5, 2009 Share Posted February 5, 2009 getcwd() Link to comment https://forums.phpfreaks.com/topic/143978-how-do-i-get-the-current-directory/#findComment-755486 Share on other sites More sharing options...
OM2 Posted February 5, 2009 Author Share Posted February 5, 2009 thanks for the quick reply getcwd() gives the full path, the same as dirname(__FILE__) for example lets say one of these calls gives me: /home/username/dir1/dir2/dir3 what i'm saying is that ALL i want is: dir3 let me know if u can suggest anything else thanks om Link to comment https://forums.phpfreaks.com/topic/143978-how-do-i-get-the-current-directory/#findComment-755499 Share on other sites More sharing options...
Mark Baker Posted February 5, 2009 Share Posted February 5, 2009 You strip out the $_SERVER["DOCUMENT_ROOT"] from the getcwd() string, which would give the current directory relative to the web root Or explode the string returned by getcwd() into an array, and take only the last element Link to comment https://forums.phpfreaks.com/topic/143978-how-do-i-get-the-current-directory/#findComment-755510 Share on other sites More sharing options...
OM2 Posted February 5, 2009 Author Share Posted February 5, 2009 u mean something like: function getCurrentDirectory() { $path = dirname($_SERVER['PHP_SELF']); $position = strrpos($path,'/') + 1; return substr($path,$position); } i was just trying to avoid having to write a function making a function call i assumed there would be a constant available? thanks Link to comment https://forums.phpfreaks.com/topic/143978-how-do-i-get-the-current-directory/#findComment-755517 Share on other sites More sharing options...
Mark Baker Posted February 5, 2009 Share Posted February 5, 2009 i was just trying to avoid having to write a function making a function call i assumed there would be a constant available? Well the current directory isn't constant you can change it $currentDirectory = array_pop(explode('/',getcwd())); Link to comment https://forums.phpfreaks.com/topic/143978-how-do-i-get-the-current-directory/#findComment-755543 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.