13

I would like to get just the name of the parent folder of which a script is currently running in the directory?

if we have a script called foo.php with a path of "/Users/Somone/Sites/public/foo.php", how can i go about just getting "public"out of the that file path and not the entire directory tree?

any help would be great.

thanks.

Moshe
  • 473
  • 1
  • 8
  • 22
  • 2
    I am unable to downvote, this has already been asked and there are tons of links to it if you do a google search. [http://stackoverflow.com/questions/1882044/get-parent-directory-of-running-script] – bowlerae Jan 23 '12 at 23:50
  • I appreciate you calling me stupid. I was not able to find a solutions to this hence i posted here. – Moshe Jan 23 '12 at 23:58
  • And i did come across the post you linked to before i posted and did not find it helpful. – Moshe Jan 24 '12 at 00:00
  • 1
    possible duplicate of [php: get the directory in which resides a file](http://stackoverflow.com/questions/5804203/php-get-the-directory-in-which-resides-a-file) @rdlowrey There are dozen better duplicates, but by Aletheia, a duplicate it is. (Also keeping in mind that PHP version 5.3 just now surpassed [20% install base](http://w3techs.com/technologies/details/pl-php/5/all), the 'most correct' solution shall be up for debate.) – mario Jan 24 '12 at 00:19

7 Answers7

22

The simplest way to do it:

basename(__DIR__);

As @mario sagely noted, this is only possible with PHP 5.3+, so if you're stuck with 5.2 or less ... well ... you should switch to a new host and stop using legacy software.

4
echo basename(__DIR__);

Edit: It appears that __DIR__ doesn't include the trailing directory separator, so the substr() call was unnecessary.

FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
1

This has worked for me:

trim(strrchr(__DIR__, DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR);

lb.
  • 5,666
  • 3
  • 17
  • 16
1
    //on windows server
$cur_dir = explode('\\', getcwd());
$sector=$cur_dir[count($cur_dir)-2];
echo $sector;
   //on linux server
$cur_dir = explode('/', getcwd());
$sector=$cur_dir[count($cur_dir)-2];
echo $sector;

Change the parent directory how you need: -2,-3,-4

Vitalicus
  • 1,188
  • 13
  • 15
  • This was asked over 3 years ago, and the user has shown no effort to solve the issue, please, always be on the lookout on the date when the question was asked. – Bonatti Nov 17 '15 at 12:46
  • 1
    it's not important if the OP doesn't see or accept this answer, it's useful for future users looking for similar solutions. – nodws Mar 16 '23 at 14:44
  • The most important on this solution is that it work with "include" function! – Vitalicus Mar 17 '23 at 20:53
0

A better way is with realpath() Returns the canonicalized absolute pathname on success. Very useful when used with relative paths.

Say you are in your www dir

realpath('.'); //will return /var/www

then you enter the /admin dir but still want the www path

realpath('..'); //will return /var/www
nodws
  • 1,018
  • 14
  • 18
0

if you pass in the script file (as the fill path to the file) as the arg for dirname(), it will return the parent directory. http://php.net/manual/en/function.dirname.php

once you have that, pass that string into strrchr to get the string that comes directly after the last slash http://www.php.net/manual/en/function.strrchr.php

strrchr(dirname(__FILE__, '/')
Kristian
  • 21,204
  • 19
  • 101
  • 176
0
substr(__DIR__, strrpos(__DIR__, '/')+1);
Czechnology
  • 14,832
  • 10
  • 62
  • 88