get parent folder paths with php

10,736

Solution 1

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.

Solution 2

First:

realpath(__FILE__)

is just useless

However, there no "real" better way, because ../ is not "dirty". The only other solution, that comes in my mind

dirname(dirname(__FILE__))

.. is the way a filesystem (its not invented by php ;)) defines its parent directory, just as well . defines the current directory.

Share:
10,736
Yev
Author by

Yev

Updated on June 09, 2022

Comments

  • Yev
    Yev almost 2 years

    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/");
    
  • Yev
    Yev about 13 years
    can you elaborate a little bit as to why you say that "realpath(__FILE__) is just useless"?
  • KingCrunch
    KingCrunch about 13 years
    @Yevgeniy: Its because __FILE__ is always absolute and because it is the filename of the file it is written in, the file it points to definitely exists. realpath(__FILE__) == __FILE__
  • Yev
    Yev about 13 years
    Although I understand the logic behind defining paths in a file in the config/ folder, I have seen it done this way (in the index.php) in other examples and tutorials. Since I will only be defining these once, in one file, I feel like this is appropriate. I am storing all of the application files outside of the public folder, to prevent any potential, unauthorized access. So in my case: BASE_PATH = "/" & PUBLIC_PATH = "/public_html/"
  • pleasedontbelong
    pleasedontbelong about 13 years
    you're right but FILE does not work in same way at every version of PHP. and after having a few problems with that i decided that i'd be happier with using just a string... now i usually have many config files where i define the constants for all the aplication, one of those files have all the constants that changes when i publish the project on a different server, and a main config file that includes them.. so if for any reason someone has to add,remove or modify a constant, he will know that the constants are on that folder.. but you know, that's just a choice =)
  • Yev
    Yev about 13 years
    I just did some research and it looks like you're right, but I'll stick to my way :) Just in case anyone is curious __FILE__ definition straight form php.net "The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances."