get parent folder paths with php

Yev picture Yev · Apr 7, 2011 · Viewed 10.7k times · Source

I am working on my first MVC framework, and want to define 4 constants for BASE_PATH, APP_PATH, LIB_PATH & PUBLIC_PATH. My file structure looks like this:

/
/app
    /controllers
    /models
    /views
/config
/db
/lib
/public_html
    /css
    /js
    /img

My index.php file is located in the public_html directory. And currently has the following code:

error_reporting(E_ALL);
define('BASE_PATH',dirname(realpath(__FILE__)) . "/../");
define('APP_PATH',dirname(realpath(__FILE__)) . "/../app/");
define('LIB_PATH',dirname(realpath(__FILE__)) . "/../lib/");
define('PUBLIC_PATH',dirname(realpath(__FILE__)) . "/");

require LIB_PATH . 'core.php';

This works, but I feel like there must be a better way of doing it without all of the "..". Any suggestions or is this the best way of going about it? Let me know. Thank you!


ANSWER

Thank you to @fireeyedboy and @KingCrunch, I have come up with the solution I was looking for. This is my final code:

define('PUBLIC_PATH', dirname(__FILE__) . "/");
define('BASE_PATH', dirname(PUBLIC_PATH) . "/");
define('APP_PATH', BASE_PATH . "app/");
define('LIB_PATH', BASE_PATH . "lib/");

Answer

Decent Dabbler picture Decent Dabbler · Apr 7, 2011

How about this:

define('PUBLIC_PATH',dirname(realpath(__FILE__)) . "/");
define('BASE_PATH',dirname(PUBLIC_PATH));
define('APP_PATH',BASE_PATH . "/app/");
define('LIB_PATH',BASE_PATH . "/lib/");

In other words use dirname() again. And re-order defining the constants to make direct use of them. Not sure it helps readability though.