The getcwd() function in PHP is an inbuilt function which is used to return the current working directory. This function does not accepts any parameter and returns the current working directory on successful function call or FALSE on failure.
Syntax:
getcwd()
Parameters: This function does not accept any parameter.
Return Value: It returns the current working directory on successful function call or FALSE on failure.
Errors And Exceptions:
- getcwd() returns FALSE on some Unix variants if any one of the parent directories does not have the readable or search mode set.
- If you try to use getcwd() in a directory that is a symbolic link, getcwd() gives you the target of that link.
Below programs illustrate the getcwd() function:
Program 1:
<?php
$cur_dir = getcwd ();
echo $cur_dir ;
?>
|
Output:
user/home/gfg
Program 2:
<?php
$cur_dir = getcwd ();
echo ( "Current Directory is " . $cur_dir );
echo "<br>" ;
$flag = chdir ( "user/home/articles" );
if ( $flag == true)
{
$cur_dir = getcwd ();
echo ( "Directory Has Been Successfully Changed to " . $cur_dir );
}
else
{
echo ( "Failed to Change Directory." );
}
?>
|
Output:
Current Directory Location is user/home/gfg
Directory Has Been Successfully Changed to user/home/articles
Reference: http://php.net/manual/en/function.getcwd.php
Last Updated :
11 Jul, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...