title: PHP Get Relative Path from Parent to Included File
body: <p><strong>I have a directory structure like this:</strong></p>
  
  <pre><code>www/
    index.php
    my-library/
      my-library.php
      assets/
        my-library.css
        images/
          loading.gif
  </code></pre>
  
  <p>I need <code>my-library.php</code> to inject stylesheets into <code>index.php</code>. To do so, I need to get the relative path from <code>index.php</code> to <code>my-library/</code> -- which in this particular case, would simply be <code>"my-library"</code>.</p>
  
  <p>From within <code>my-library.php</code>, is it possible for me to acquire this relative path?</p>
  
  <p>Or must index.php supply it, with something like the following?</p>
  
  <pre><code>&lt;?php
  require "my-library/my-library.php";
  $mlib-&gt;linkroot='my-library';
  ?&gt;
  </code></pre>
  
  <p>To clarify, below I have included a more detailed representation of what I'm trying to do:</p>
  
  <p><strong>index.php:</strong></p>
  
  <pre><code>&lt;?php require "my-library/my-library.php"; ?&gt;
  &lt;!doctype html&gt;
  &lt;head&gt;
    &lt;title&gt;Testing My Library&lt;/title&gt;
    &lt;?php $mlib-&gt;injectAssets(); ?&gt;
  &lt;/head&gt;
  &lt;body&gt;..&lt;/body&gt;
  </code></pre>
  
  <p><strong>my-library.php:</strong></p>
  
  <pre><code>&lt;?php
  class MyLibrary(){
    public $rootpath;
    public $linkroot;
    function __construct(){
      $this-&gt;rootpath= __DIR__; // absolute path to my library's root directory (for serverside use)
      $this-&gt;linkroot = "???"; // relative path to my library's root from index.php (for clientside use, like linking in stylesheets)
    }
    function injectAssets(){
      $csslink = $this-&gt;linkroot.'/assets/my-library.css';
      echo '&lt;link href="'.$csslink.'" rel="stylesheet" /&gt;';
    }
  }
  $mlib = new MyLibrary();
  ?&gt;
  </code></pre>
  
  <p>The line I'm interested in figuring out, would be <strong><code>$this-&gt;linkroot = "???";</code></strong>.</p>
  
  <p>I'm practically trying to acquire the string that was used to include/require the current script.</p>