Tags
PHP , Lamp
Asked 7 years ago
28 Nov 2016
Views 3198
lain

lain posted

how to get path of directory in php

i have file and i want full physical directory path of that file . like /home2/shareddomain/public_html

so how can i get full directory path by php programm or by Cpanel ? any how i need directory path so i can use it to set cron .

http://arrayoverflow.com/question/how-to-set-cron-in-cpanel/157

i am using Lamp shared server
ravi

ravi
answered Nov 30 '-1 00:00

make file name directoryname.php and copy/paste below code and run it .


<?php 
echo $_SERVER['DOCUMENT_ROOT'];
?>

echo $_SERVER['DOCUMENT_ROOT'] give you root directory path which is physical directory path
Mitul Dabhi

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
Post Answer