1

I have a scenario where I want to get a path back to a specific parent directory. Here is an example folder strcuture ([something] is a folder)

index.php
[components]
    header.php
    footer.php
[pages]
    somePage.php
    [somePageSubPages]
        someSubPage.php

So my content pages look something like this:

<?php
    include('components/header.php');
?>

<!-- CONTENT STUFF -->

<?php
    include('components/footer.php');
?>

This works for the index.php but not for somePage.php and someSubPage.php. What I want to do is create a function that returns the path back to the main directory so I can then add this to the includes and other stuff:

$relPath = getPathToRoot($rootDirName);
include($relPath . 'components/header.php');

And the function only would return an empty string or ../../.

I thought about using __FILE__ and then just count the /-characters between the given $rootDirName and the the string end. However, I would like to ask if this is a reliable way and how this would look in PHP. (I don't realy work that much with PHP...)

ruhig brauner
  • 943
  • 1
  • 13
  • 35
  • 1
    possible duplicate of http://stackoverflow.com/questions/3429262/get-base-directory-of-current-script – rahulmishra Aug 20 '14 at 20:33
  • where would you define such function? After all, you'd need to include the file containing such function, so you would be back to the original question. – ffflabs Aug 20 '14 at 20:41
  • I've noticed this dilemma :) I think the line-count would be small enough to fit in the content-pages. Not elegant but should work. @rahulmr, thanks. Somehow skipped this in the google search. – ruhig brauner Aug 20 '14 at 20:46

2 Answers2

3

You can use .. to get into the parent directory:

<?php
    // somePage.php
    include('../components/header.php');
?>
jh314
  • 27,144
  • 16
  • 62
  • 82
1

I would make sure that you always know the root of your site so that you can take it from there.

To do that, you could go at least two ways:

  • Include a config file that defines your site-root and prepend that root to every include like (in case of a constant):
    include MY_SITE_ROOT . '/path/to/file.php';
  • Use a server variable to achieve the same like:
    include $_SERVER['DOCUMENT_ROOT'] . '/path/to/file.php';
jeroen
  • 91,079
  • 21
  • 114
  • 132
  • Would work but I would like to not use an absolute path to my root just for portability etc. ;) – ruhig brauner Aug 20 '14 at 20:44
  • 1
    @ruhigbrauner I think a one-time setup (or server variable) is a lot better for portability and flexibility than using relative paths. Relative paths always get you into trouble sooner (like now...) or later :-) – jeroen Aug 20 '14 at 20:47
  • 1
    The website is pure HTML with a bit of PHP for includes and conditions etc. So I will notice when there is a problem. :) But you are right, I've also learned that relative paths should be avoided in real projects. – ruhig brauner Aug 20 '14 at 20:52
  • 1
    Just do `define('MY_SITE_ROOT', dirname(__FILE__));` in a central (config) file and you have the root directory (it is the root of the script in which the definition is). And always use absolute filepaths. – Charlotte Dunois Aug 20 '14 at 21:40