PHP | getcwd( ) Function

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:

  1. getcwd() returns FALSE on some Unix variants if any one of the parent directories does not have the readable or search mode set.
  2. 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
//current directory
$cur_dir = getcwd();
  
// displaying current directory
echo $cur_dir
?>

chevron_right


Output:

user/home/gfg

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
//current directory
 $cur_dir = getcwd();
  
echo("Current Directory is " . $cur_dir);
  
echo "<br>" ;
  
//changing current directory
$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.");
}
?>

chevron_right


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



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.