33

How can I return the pathname from the current file, only 2 directories up?

So if I my current file URL is returning theme/includes/functions.php

How can I return "theme/"

Currently I am using

return dirname(__FILE__)

7 Answers 7

97

PHP 5.3+

return dirname(__DIR__);

PHP 5.2 and lower

return dirname(dirname(__FILE__));

With PHP7 go further up the directory tree by specifying the 2nd argument to dirname. Versions prior to 7 will require further nesting of dirname.

http://php.net/manual/en/function.dirname.php

5
  • Is the main benefit from this method over dirname(__FILE__).'/../'; to remove the possible inconsistency of DIRECTORY_SEPARATOR?
    – Patrick
    Apr 27, 2012 at 14:04
  • 1
    @Patrick I would say that the main benefit of this over your suggestion is that we get the absolute path of the directory instead of a relative path. Also, DIRECTORY_SEPARATOR inconsistencies are generally edge cases as PHP automatically converts *nix style separators into the appropriate Windows style separator for most cases. Apr 27, 2012 at 14:12
  • 4
    what if you had to go 3+ levels up? Would be nice if dirname's 2nd parameter was $levels, thus dirname(__FILE__, 3); :-)
    – Geo
    Sep 9, 2014 at 16:09
  • 1
    @Patrick another strategy to ensure the correct DIRECTORY_SEPARATOR is uses is to wrap the path in realpath() Oct 2, 2014 at 22:08
  • 2
    From PHP 7 there is a second parameter levels. php.net/manual/en/function.dirname.php Oct 11, 2015 at 14:12
39

Even simpler than dirname(dirname(__FILE__)); is using __DIR__

dirname(__DIR__);

which works from php 5.3 on.

4
[ web root ]
    / config.php
    [ admin ] 
        [ profile ] 
            / somefile.php 

How can you include config.php in somefile.php? You need to use dirname with 3 directories structure from the current somefile.php file.

require_once dirname(dirname(dirname(__FILE__))) . '/config.php'; 

dirname(dirname(dirname(__FILE__))) . '/config.php'; # 3 directories up to current file
2

As suggested by @geo, here's an enhanced dirname function that accepts a 2nd param with the depth of a dirname search:

/**
 * Applies dirname() multiple times.
 * @author Jorge Orpinel <[email protected]>
 * 
 * @param string $path file/directory path to beggin at
 * @param number $depth number of times to apply dirname(), 2 by default
 * 
 * @todo validate params
 */
function dirname2( $path, $depth = 2 ) {

    for( $d=1 ; $d <= $depth ; $d++ )
        $path = dirname( $path );

    return $path;
}

Note: that @todo may be relevant.

The only problem is that if this function is in an external include (e.g. util.php) you can't use it to include such file :B

2

Late to the party, but you can also do something like below, using \..\..\ as many times as needed to move up directory levels.

$credentials = require __DIR__ . '\..\App\Database\config\file.php';

Which is the equivalent to:

$credentials = dirname(__DIR__) . '\App\Database\config\file.php';

The benefit being is that it avoids having to nest dirname like:

dirname(dirname(dirname(__DIR__))

Note, that this is tested on a IIS server - not sure about a linux server, but I don't see why it wouldn't work.

1
  • Also look at using realpath() like realpath(__DIR__ . '/../../') to go up two levels.
    – puiu
    May 12, 2016 at 18:57
0
require_once(dirname(__FILE__) . "/../../functions.php");
0

This is an old question but sill relevant.

Use:

basename(dirname(__DIR__));

to return just the second parent folder name - "theme" in this case.

http://php.net/manual/en/function.basename.php

1
  • (instead of dirname(dirname(__DIR__)); which returns to full path from / upto the second parent folder) Nov 29, 2016 at 19:45

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.