SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_SERVER['DOCUMENT_ROOT'] OR realpath()?

    Usually I use this to define the path to directory which contains the scripts:
    realpath(dirname(__FILE__))

    so at the very beginning of the index.php I can put something like this:
    define('SITE_PATH', realpath(dirname(__FILE__)));

    Then I can use SITE_PATH inside any class anytime I need to get value of root dir, so If I need to get absolute path to 'images' directory, I would use
    SITE_PATH_PATH . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR;

    This works, but I saw another post where someone asked about the use of
    $_SERVER['DOCUMENT_ROOT']
    the $_SERVER['DOCUMENT_ROOT'] is doing pretty much the same thing as realpath(dirname(__FILE__)) right?

    Now I am wondering, when should I use realpath(dirname(__FILE__)) and when to use $_SERVER['DOCUMENT_ROOT']?

    Which one is the preferred way to get the path of my scripts root directory?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If your script is installed in its own directory, it won't be in the document root, and the two methods will return different paths.

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You do not need realpath in the above code. __FILE__ returns an absolute path, not a relative path.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I also see realpath(dirname(__FILE__)) used by a couple frameworks in establishing the root directory in the front controller. There must be a reason for that.

  5. #5
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    You do not need realpath in the above code. __FILE__ returns an absolute path, not a relative path.
    Interesting observation. I originally copied this path translation line of code from Zend framework's tutorial here:
    http://framework.zend.com/manual/en/...ssloading.html

    I guess they are wrong to use it?

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Zend is doing: realpath( dirname(__FILE__) . '/../library' ), Notice '/../library' is being appeneded to dirname's output before realpath? If you are just using dirname and __FILE__ realpath is surpflouse. Zend is using realpath to transform the relative path ('/../library') appended into absolute.

    Edit: Well scrolling down a bit more I see it now...cannot tell you why Zend is suggesting that bit its not needed.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Zend is doing: realpath( dirname(__FILE__) . '/../library' ), Notice '/../library' is being appeneded to dirname's output before realpath? If you are just using dirname and __FILE__ realpath is surpflouse. Zend is using realpath to transform the relative path ('/../library') appended into absolute.

    Edit: Well scrolling down a bit more I see it now...cannot tell you why Zend is suggesting that bit its not needed.
    Yes, in one place, but on the same page they have example with just
    define('APPLICATION_PATH', realpath(dirname(__FILE__)));
    I think realpath() is still necessary in case there are any symlinks in the path returned by __FILE__

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,135
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    If document root is supported that's what I would recommend using. That is if you would like a path to the site root.

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Symbolic links don't pose a problem. I symlink a lot of directories and relative paths / absolute paths to symlinked directories work fine.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •