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
- Save the above PHP script to a file, for example path.php,
- Upload the file path.php to the directory of which you want to know the path,
- Launch a web browser and navigating to the script e.g. http://www.example.com/directory/path.php
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.
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.
Remove !– and — at each end when using.
That looks like it had to be added to display and not run the code.
Thank you for pointing out the mistake. It was indeed a formatting mistake, now it is fixed.
Sorry, but I only see the first line of the 3-line PHP script above for locating the absolute path of a directory.
There is only one line of code Tad. Here it is:
Hope this helps.