49

I am using this:

echo dirname(__FILE__);

which gives:

C:\UwAmp\www\myfolder\admin

However I am looking for path until:

C:\UwAmp\www\myfolder\

from current script. How can that be done ?

7 Answers 7

96

You could do either:

dirname(__DIR__);

Or:

__DIR__ . '/..';

...but in a web server environment you will probably find that you are already working from current file's working directory, so you can probably just use:

'../'

...to reference the directory above. You can replace __DIR__ with dirname(__FILE__) before PHP 5.3.0.

You should also be aware what __DIR__ and __FILE__ refers to:

The full path and filename of the file. If used inside an include, the name of the included file is returned.

So it may not always point to where you want it to.

1
  • What do you actually want to do with the path? Are you trying to include a file, use it in a link etc?
    – DaveRandom
    Feb 5, 2012 at 13:13
22

You can try

echo realpath(__DIR__ . DIRECTORY_SEPARATOR . '..'); 
5
  • I get empty string with that.
    – Dev555
    Feb 5, 2012 at 13:12
  • This shouldn't give you an empty string ... Do you use PHP 5.3?
    – Dan Soap
    Feb 5, 2012 at 13:18
  • php 5.2 is what i am using +1 for your help though.
    – Dev555
    Feb 5, 2012 at 13:21
  • That's it, clean & simple. This should be the accepted answer. Thank you Dan! Jun 5, 2019 at 17:33
  • Works for me with PHP 7.
    – Eje
    Sep 24, 2019 at 5:38
8
echo dirname(__DIR__);

But note the __DIR__ constant was added in PHP 5.3.0.

7

Also you can use dirname(__DIR__, $level) for access any folding level without traversing

1
  • To add to this answer, the second parameter of dirname() is PHP7+ only. It's an integer representing how many levels to go up.
    – BadHorsie
    Nov 30, 2020 at 16:01
5

The parent directory of an included file would be

dirname(getcwd())

e.g. the file is /var/www/html/folder/inc/file.inc.php which is included in /var/www/html/folder/index.php

then by calling /file/index.php

getcwd() is /var/www/html/folder  
__DIR__ is /var/www/html/folder/inc  
so dirname(__DIR__) is /var/www/html/folder

but what we want is /var/www/html which is dirname(getcwd())

2

To Whom, deailing with share hosting environment and still chance to have Current PHP less than 7.0 Who does not have dirname( __FILE__, 2 ); it is possible to use following.

function dirname_safe($path, $level = 0){
    $dir = explode(DIRECTORY_SEPARATOR, $path);
    $level = $level * -1;
    if($level == 0) $level = count($dir);
    array_splice($dir, $level);
    return implode($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
}

print_r(dirname_safe(__DIR__, 2));
2

Use $_SERVER['DOCUMENT_ROOT']

For example if you want to access a folder above the root directory from anywhere

$_SERVER['DOCUMENT_ROOT']."/../myfolder/spl-auto.php"

or for your example C:\UwAmp\www\myfolder\admin

$admin = $_SERVER['DOCUMENT_ROOT']."/myfolder/admin/"
$myfolder = $_SERVER['DOCUMENT_ROOT']."/myfolder/"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.