How to find the full path to a file using PHP

Sometimes you need to know the full path to a file or directory, for example, if you want to setup .htaccess authentication you need to enter the full path to the .htpasswd file. If you dont know the full path and PHP is installed on the server, we will show you how you can find this information.

Below is a small PHP script that prints the full path to the directory it is placed in. Copy the code and paste it into a file called test.php. You can then upload the file to the directory where you want to place the .htpasswd. Then point your browser to http://www.your-domain.com/path/to/test.php


<?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>";
?>

 

Nathan da Silva - Profile

Posted by: Nathan da Silva

Nathan is the Founder of Silva Web Designs. He is passionate about web development and web site design. His expertise is WordPress & Magento as well as many other frameworks. Would you like to work with Nathan? Send him an email on [email protected]

It’s good to share