How to Find the Absolute Path on a Webserver Using PHP

Last updated on October 18th, 2014 by Robert Abela. Filed under WordPress 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 to password protect your WordPress wp-admin directory, 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

WP White Security is hosted on A2 Hosting, protected with BBQ:Block Bad Queries Firewall and backed up with BlogVault online WordPress backup service

4 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.

Leave a Reply

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