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. www.yourwebserver.com/yourfolder/)
  • open your webbrowser and go to the location you´ve uploaded the file (f.e. www.yourwebserver.com/yourfolder/fullpath.php)
  • enjoy your relative path (which could look f.e. something like this  ‘/var/www/vhosts/yourwebserver.com/httpdocs/yourfolder’)
  • 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/yourwebserver.com/httpdocs/yourfolder/’)

Source: http://www.htaccesstools.com/articles/full-path-to-file-using-php/

Leave a Reply

Your email address will not be published. Required fields are marked *