Get just parent folder name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
arpowers
Forum Commoner
Posts: 76
Joined: Sun Oct 14, 2007 10:05 pm
Location: san diego, ca

Get just parent folder name

Post by arpowers »

Hey Guys!~

I have a question:

I have a directory structure like this:
http://www.root.com/application/script.php

I need to figure out a way to get just the name of the parent folder (not the entire path) of a script. (in above, A quick function that returnd just 'application' would be great!)

This will allow me to define this:
define('CURRENT_APP', need this code...);

this will allow me to dynamically determine which application I am in and will help me redact the code.

What is the best way to do this?

I am familiar with all the functions that return the entire directory path you are in...

THANKS:D
ANDREW
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Get just parent folder name

Post by Christopher »

Something like:

Code: Select all

array_pop(explode('/', dirname($_SERVER['SCRIPT_PATH'])))
I don't recall if those are all the right function names.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Get just parent folder name

Post by alex.barylski »

Code: Select all

 
 
$path = 'this/is/an/example/file.dat';
 
$parent = basename(dirname($path));
 
echo $parent; // Result: example
 
 
User avatar
arpowers
Forum Commoner
Posts: 76
Joined: Sun Oct 14, 2007 10:05 pm
Location: san diego, ca

Re: Get just parent folder name

Post by arpowers »

Good answer arbor ! 5 gold stars...

I didn't think about using explode and array_pop.. pretty simple:)

thanks for your help, It seems like you are always showing me the way.

anybody else has a solution please post it...

Ap
User avatar
arpowers
Forum Commoner
Posts: 76
Joined: Sun Oct 14, 2007 10:05 pm
Location: san diego, ca

Re: Get just parent folder name

Post by arpowers »

thanks Hockey.. even better:)
Post Reply