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

Robert Douglas robert.douglas at rightmove.co.uk
Wed Dec 20 04:51:57 CST 2006

Hey Stephen,

Using the 'explode' function on your url creates an array of your
directories - you can then access each of the directory names

$urlToExplode = dirName($_SERVER['PHP_SELF']); // This gives us just the
path of the current page (in your case '/main/something/inner')
$delimiter = "/";
$explodedUrl = explode(delimiter,urlToExplode);
$middleDirectory = $explodedUrl[1];

It may not be the most elegant solution and you could reduce it to a
couple lines, but as a beginner it makes sense to me as I can see the
component parts as I debug.

Good luck,


Message: 2
Date: Tue, 19 Dec 2006 11:54:23 -0600
From: Stephen Rider <evolt_org at striderweb.com>
Subject: [thelist] PHP - get one directory from a path?
To: thelist at lists.evolt.org
Message-ID: <38510AEB-E7A6-4E0F-BC30-30322698EB61 at striderweb.com>
Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed

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/ 
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

This email has been scanned for all viruses by the MessageLabs service. 

More information about the thelist mailing list