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');
?>
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.
basename(getcwd()) approach above is shorter and avoids choosing a separator manually.
<?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];
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 " ;
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.