getcwd(): Gets the current working directory

Get the working directory without platform-specific string splitting

getcwd() returns the current working directory or false on failure. If you only need the final directory name, basename() avoids manually splitting Windows and Linux paths.

<?php
$currentDirectory = getcwd();

if ($currentDirectory === false) {
    throw new RuntimeException('Unable to read the current working directory.');
}

echo htmlspecialchars($currentDirectory, ENT_QUOTES, 'UTF-8');
echo '
';
echo htmlspecialchars(basename($currentDirectory), ENT_QUOTES, 'UTF-8');
?>
About chroot(): chroot() is a specialized server operation with operating-system and permission requirements. It is not a normal way to navigate folders in an application. Prefer explicit application paths for regular file handling.
echo getcwd();
The output is here in Linux server
/home/myuserid/public_html 
In a windows platform the output will be
J:\php_files\t\dir
Getcwd returns current directory name with path. This function returns false if it failed to get any result.

Older path-splitting approach

The following older example demonstrates manual path splitting with explode(). For new code, the basename(getcwd()) approach above is shorter and avoids choosing a separator manually.

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];

Specialized chroot() example

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 " ;
}


Directory Functions Change current Directory to a new one


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer