Mitul Dabhi
answered Nov 30 '-1 00:00
by many way you can get directory path .
1. get directory path by dirname function in php
you need to pass file name argument so it will return directory path of given file name.so if you pass current file name by __FILE__ , it current directory where file is reside and running now .
<?php
echo dirname(__FILE__);// return current file 's directory.
?>
Note :: __FILE__ constant refer to real current file path in php
2. Get the directory from __FILE__
echo str_replace(basename(__FILE__),"",__FILE__);
__FILE__ give us full real path like /home2/shareddomain/public_html/realpath.php
basename function give us only basefile name like realpath.php
so if we remove basename from __FILE__ , it remain /home2/shareddomain/public_html
Note :: but not advisable to use because its good to use dirname function direct
3. $_SERVER['DOCUMENT_ROOT']
$_SERVER['DOCUMENT_ROOT'] give document root directory under which the current script is executing , it means it give main root folder like if you script running in the /home2/shareddomain/public_html/cron/main.php than it return path like /home2/shareddomain/public_html
echo $dir=$_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'cron';
so append directory name as you need to document root path.
Note :: DIRECTORY_SEPARATOR is advisable to use because it work for both linux or window environment