How to Get Absolute Directory Path in PHP Script?

PHP 4.0 introduced __FILE__ magic constant, which provides the full path and filename of the file.

 

To get directory path only use dirname(__FILE__). For example to include other file in same directory use:

<?php
include(dirname(__FILE__)."/other.php");
?>

 

Later the PHP version 5.3.0 and later versions introduced a new constant called __DIR__ which is the short form of the dirname(__FILE__). You can also use this in replacement of what we used earlier.

 

Now, you can use the above script like:

<?php
 include(__DIR__."/other.php");
?>

 

To simply print the absolute directory path of PHP script located in:

<?php
echo __DIR__;
?>

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)