Getcwd returns current directory name with path. This function returns false if it failed to get any result.
Getting current directory name
By using getcwd() function we will get path to the current directory. From this path we can take out present working directory by using explode function
By using explode() string function we will break the path by using \ as delimiter. In this array the list element will be the current directory name. So we will find out the total number of elements present in the array by using count() function. We will subtract 1 from total element ( because the first element starts with 0 , not 1 ) to get the last element.
Here is the code.
<?Php
$dir_path=getcwd();
//echo $dir_path;
$present_dir = explode('\\', getcwd()); # use delimiter based on your path
echo $present_dir[count($present_dir)-1];
?>
The output is here
dir
To display all the elements we can use like this.
print_r($present_dir);
Above code will work for windows system, however for Linux system we have to change explode like this.
$present_dir = explode('/', getcwd());
We can find out the userid of the hosting account from this
echo $present_dir[2];
Root directory by chroot() function
We can shift from current directory to root directory by using chroot function. This function will return true of false based on the outcome. You must have sufficient permission to execute this function. In windows platform this function will not work. So before using it is better to check this function. Here is an simple example.
echo getcwd(); // get the current location
chroot('H:\php_files\t\dir');
echo "<br>";
echo getcwd();
It is better to check the function by using function_exists. Here is the code
if ( function_exists("chroot") ){
chroot($chroot);
echo getcwd();
}else{
echo " this is not supported " ;
}