Finding the absolute path of a directory on a website (using PHP)

Last updated on April 21st, 2020 by Robert Abela. Filed under WordPress Admin Tips

A WordPress website is made up from a number of files, organized in a number of sub directories. These files and sub directories are saved in a directory on a web server. This is the root directory of your site, also known as the document root.

Sometimes you need to find out absolute path of a directory on your WordPress site. The absolute path is the path of where a particular file or directory is saved on the web server. For example, if you are using .htaccess file restrict access to the WordPress dashboard, you need to specify the absolute path of the .htpasswd file (Apache password file) in the .htaccess file, otherwise the authentication files cannot be found.

This short article explains how you can find the absolute path of a website directory.

PHP script to find absolute path

Here is a simple one line PHP script you can use to find the absolute path of the directory on your web server.

<?php echo getcwd(); ?>

How to find the absolute path of a directory on your website

  1. Save the above PHP script to a file, for example path.php,
  2. Upload the file path.php to the directory of which you want to know the path,
  3. Launch a web browser and navigating to the script e.g. http://www.example.com/directory/path.php

WordPress Hosting, Firewall and Backup

This Website is:

6 comments

Nate 20/08/2012

Whats suppose to happen when I navigate to script?
All I’m getting is a blank screen with no sign of a root file path.

Please help!

John Bonello 20/08/2012

Hi Nate,

You should simply get the path where the script is uploaded. E.g. in my test installation of Xampp on windows it simply shows the following: C:\xampp\htdocs\xampp

Please note that if you use the function “copy to clipboard” from this blog post, in notepad it will be shown as one line, which is not the correct format. I recommend using the “view source” option and then paste it on the text editor.

Garry 15/08/2014

Remove !– and — at each end when using.
That looks like it had to be added to display and not run the code.

WP White Security 15/08/2014

Hi Garry,

Thank you for pointing out the mistake. It was indeed a formatting mistake, now it is fixed.

Tad 14/08/2019

Sorry, but I only see the first line of the 3-line PHP script above for locating the absolute path of a directory.

Robert Abela 22/08/2019

There is only one line of code Tad. Here it is:

Hope this helps.

Leave a Reply

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