detecting directory

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
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

detecting directory

Post by m2babaey »

Hi
I need a command that returns the directory name that my file is in
for example if my file is www/javad/codes/getdir.php , then I want to place a code in getdir.php that returns "codes" (and if possible "javad")
Thanks for your help
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: detecting directory

Post by requinix »

__FILE__ is a magic constant that gives you the full path to the file it's used it. With some combination of dirname and basename...

Code: Select all

// __FILE__ is /path/to/file.php
echo basename(__FILE__); // file.php
echo basename(dirname(__FILE__)); // to
echo basename(dirname(dirname(__FILE__))); // path
echo basename(dirname(dirname(dirname(__FILE__)))); // .
Post Reply