getcwd()

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

/home/myuserid/public_html

In a windows platform the output will be

J:\php_files\t\dir

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());
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 findout 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 " ;
}

Be the first to post comment on this article :


Google+

Post Comment This is for short comments only. Use the forum for more discussions.






OPINION POLL

What is the most important factor of a web site
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2015 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer