Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find current directory level with PHP

Tags:

php

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?

like image 358
Marcus Avatar asked Oct 07 '11 23:10

Marcus


1 Answers

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';
like image 74
zerkms Avatar answered Sep 20 '22 22:09

zerkms