Getcwd returns current directory name with path. This function returns true on successful and returns false if it failed to get any result.
Here is a sample code.
echo getcwd();
The output is here in Linux server
In a windows platform the output will be

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.
//echo $dir_path;

$present_dir = explode('', getcwd());
echo $present_dir[count($present_dir)-1];

The output is here
To display all the elements we can use like this.
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
echo "<br>";
echo getcwd();
It is better to check the function by using function_exists. Here is the code
if ( function_exists("chroot") ){
echo getcwd();
echo " this is not supported " ;

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here .

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 All rights reserved worldwide Privacy Policy Disclaimer