Contact!

Move up directory levels relative to the current file in PHP

Moving up and down directory levels from the current file in PHP

I often find that simple things are the ones most forgotten or unknown. I’ve recently been helping colleagues learn PHP, and I frequently get asked how to move up and down directory levels in relation to the current file.

I know of two relatively simple ways to do this; one using dirname()
and one using realpath().

We will also be using ‘Magic Constants’ in this post, but handily I’ve explained these a bit here.

Directory levels using dirname()

If you are using PHP 7.0 and above then the best way to navigate from your current directory or file path is to use dirname(). This function will return the parent directory of whatever path we pass to it. In PHP 7 and above we can specify how many levels we would like to move. To use this we first pass the file or directory path as a string, and then specify how many levels we would like to move up from that directory.

Let’s assume that our script is running in /path/to/file/index.php:

$dir = dirname(__DIR__, 2);

echo $dir; // Result = "/path"

By using the ‘Magic Constant’ ‘__DIR__’ and specifying a level of 2, we are asking dirname() to return 2 levels up from the parent of ‘__DIR__’. In this case ‘__DIR__’ is ‘/path/to/file’, so 2 levels higher is ‘/path’.

Remember: dirname() returns the parent directory, so using ‘dirname(__DIR__)’ will return the parent directory of ‘__DIR__’.

If we use ‘__FILE__’ it is slightly different:

$dir = dirname(__FILE__, 2);

echo $dir; // Result = "/path/to"

Because ‘__FILE__’ returns the file in the string, the parent directory is ‘/path/to/file’. By specifying a level of 2 we now move to ‘/path/to’ instead.

Directory levels using realpath()

If you happen to be using an older version of PHP then luckily we can use realpath().

The ‘Magic Constant’ ‘__DIR__’ was added to PHP in 5.3, so for people 5.3 to 5.6 we can move directory levels like this:

Again, assuming that our script is running in /path/to/file/index.php:

$dir = realpath(__DIR__ . '/..');

echo $dir; // Result = "/path/to"

In this example, realpath() resolves the reference ‘/..’ in relation to ‘__DIR__’ and moves us 1 level higher. If we were to use ‘/../..’ then we would move 2 levels higher, and so on.

If we are going back even further than PHP 5.3 then we would have to use realpath(), dirname() and the ‘Magic Constant’ ‘__FILE__’.

Essentially, realpath() won’t accept a file, only a path. This means that we have to remove the ‘index.php’ from our ‘__FILE__’ string. There are a few ways that we could achieve this, one of them is using dirname().

$dir = realpath(dirname(__FILE__) . '/..');

echo $dir; // result = "/path/to"

This example takes the parent directory of ‘__FILE__’ and then resolves ‘/..’ using realpath() to move it 1 level higher.

Join the discussion!

You might like:

Create, register and use shortcodes in WordPress

Create, register and use shortcodes in WordPress

by Gav 18/03/2023

Learn how to create and register your own WordPress shortcodes to add dynamic content to your posts and pages.

How to use guard clauses in JavaScript

How to/why use guard clauses in JavaScript

by Gav 16/03/2023

Learn how to improve code readability and performance by using guard clauses in JavaScript. Discover their benefits and best practices.

Implements and Extends, Object Oriented TypeScript

Implements and Extends, Object Oriented TypeScript

by Gav 15/03/2023

Learn the difference between implements and extends in TypeScript. Use Implements to implement interfaces and types, and extends to inherit from classes.

Reading/Parsing and Writing YAML files in PHP, Symfony

Reading/Parsing and Writing YAML files, PHP Symfony

by Gav 14/03/2023

In this tutorial we will look at using YAML in PHP. Learn about Parsing and Writing YAML files using Symfony's YAML component.

Measuring code execution performance in JavaScript

Measuring code execution performance in JavaScript

by Gav 13/03/2023

Measuring code execution performance is an important way to identify bottlenecks. Use these methods in JavaScript to help optimise your code.

Measuring script/code execution time in PHP using microtime

Measuring script/code execution time in PHP, microtime

by Gav 06/03/2023

Find bottlenecks, optimise and clean your code, and speed up your apps by measuring the execution time of your PHP scripts using microtime.

Regenerate WordPress media image sizes, programmatically

Regenerate WordPress media image sizes, programmatically

by Gav 25/02/2021

Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.

Magic Constants in PHP. What they are and how to use them.

Magic Constants in PHP. What they are and how to use them

by Gav 15/02/2021

Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.

Detecting Keypress JavaScript

Detect single and multiple keypress events: JavaScript

by Gav 16/10/2019

Learn how to use event listeners to detect and handle single and multiple keypress events in JavaScript. Add modifier keys to your application!