I need to require_once a file that is located in my sites root directory. The problem I have is that I won't always know how many levels up the root directory is from where my current script is running. So I need to figure out how many directory levels I need to go up to require_once my file.
Some times it could be:
require_once '../../file.php';
And some times it could be:
require_once '../file.php';
Or any other number of directory levels up.
How can I calculate how many "../" I need to get to the root directory from any location in a sub directory?
Create a constant SITE_ROOT
in your index.php
and use it everywhere you need to specify the path, like:
require_once SITE_ROOT . '/path/from/siteroot/to/file.ext';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With