Howto get the relative path of a file / folder in Unix / Linux with PHP

Sometimes you´ll need the relative path of a file or folder stored on your webserver. Especially if you´re one a shared environment this can be a bit tricky sometimes.

Here is a short and easy solution for your problem:

  • create a php file named ‘fullpath.php’
  • open the file and enter the following php code
  • <?php
    $dir = dirname(__FILE__);
    echo "<p>Full path to this dir: " . $dir . "</p>";
    echo "<p>Full path to a .htpasswd file in this dir: " . $dir . "/.htpasswd" . "</p>";
  • upload the file to the folder on your webserver which you want to know the path (f.e.
  • open your webbrowser and go to the location you´ve uploaded the file (f.e.
  • enjoy your relative path (which could look f.e. something like this  ‘/var/www/vhosts/’)
  • Note: if you plan to use this relative path for other applications you may need to add an additional ‘/’ at the end (‘/var/www/vhosts/’)