How to Find the Absolute Path on a Webserver Using PHP

Last updated on April 29th, 2019 by Robert Abela. Filed under WordPress Admin Tips

Sometimes you need to know the full path or absolute path of a directory or a file stored on a 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 .htacecss file or the authentication does not work.

Below is a simple three line PHP script which you can use to find out the absolute path of the directory where it is uploaded.

<?php echo getcwd(); ?>

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

  1. Launch notepad or any text editor of your choice
  2. Copy the above script to the new file
  3. Save the file as path.php
  4. Using an FTP client, upload path.php to the directory which you want to know the path of
  5. Using a web browser, navigating to the script e.g. http://www.website.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: < ?php echo getcwd(); ?>

Hope this helps.

Leave a Reply

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