[thelist] PHP - get one directory from a path?

Phil Turmel pturmel-webdev at turmel.org
Thu Dec 21 20:02:42 CST 2006

Stephen Rider wrote:
> Hi --
> I'm trying to figure out a straightforward way to set a PHP variable  
> to a particular directory in a path.
> For example:  I have http://www.example.com/main/something/inner/ 
> index.php
> The path can change, though.  Specifically, the directory "something"  
> is arbitrary -- the script could be running in http://www.example.com/ 
> main/whatever/inner/ and so forth.
> When the script runs, I want to set a variable to = "something", or  
> whatever that directory name happens to be.
> I know how to get the last directory name using dirname() and such,  
> but I can't figure out how to get the directory two levels up....
> Any ideas?  Thanks in advance!
> Stephen Rider

I know I'm a little late to this party (business trip), but I didn't see 
anything like the one-liner that occurred to me:

$something = basename( dirname( dirname( $_SERVER['PHP_SELF'] )));

Anyways, thought I'd throw that in before the thread got really stale.


More information about the thelist mailing list