In this post I'll show you how to change the aspect ratio of an image or element using CSS, and explain why this is important.
Move up directory levels relative to 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.
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.
You might like:
Here is how you can fix node incompatibility error, using "Node Sass does not yet support your current environment" as an example.
Need to make a directory in PHP? No Problem! mkdir has you covered... Need to recursively make directories in PHP? mkdir has you covered, too!
Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.
Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.