ShareThis

Monday, August 27, 2012

[PHP] Get working directory

In PHP, there is a very simple way to retrieve the working directory.

You simply call dirname(__FILE__ wherever it is needed. You can also stack dirnames to retrieve parent directories as shown in the example below:

<?php
 
//Example script path: home/content/en/script.php
 
$parentparentdir=basename(dirname(dirname(__FILE__)));
 echo 
$parentparentdir//will output 'content'

?>

0 comments:

Post a Comment